Lost's revenge
Total Submission(s): 3092 Accepted Submission(s): 813
One noon, when Lost was lying on the bed, the Spring Brother poster on the wall(Lost is a believer of Spring Brother) said hello to him! Spring Brother said, "I'm Spring Brother, and I saw AekdyCoin shames you again and again. I can't bear my believers were being bullied. Now, I give you a chance to rearrange your gene sequences to defeat AekdyCoin!".
It's soooo crazy and unbelievable to rearrange the gene sequences, but Lost has no choice. He knows some genes called "number theory gene" will affect one "level of number theory". And two of the same kind of gene in different position in the gene sequences will affect two "level of number theory", even though they overlap each other. There is nothing but revenge in his mind. So he needs you help to calculate the most "level of number theory" after rearrangement.
For each testcase, first line is number of "number theory gene" N(1<=N<=50). N=0 denotes the end of the input file.
Next N lines means the "number theory gene", and the length of every "number theory gene" is no more than 10.
The last line is Lost's gene sequences, its length is also less or equal 40.
All genes and gene sequences are only contains capital letter ACGT.
int A,C,G,T;
int exsys(int a,int c,int g,int t)
{
int BT=1;
int BG=BT*(T+1);
int BC=BG*(G+1);
int BA=BC*(C+1);
return a*BA+c*BC+g*BG+t;
}
#include<cstdio>
#include<cstring>
#define SIGMA_SIZE 4
#define maxn 520
#include<queue>
using namespace std;
int ch[maxn][SIGMA_SIZE];
int val[maxn];
int last[maxn], f[maxn];
int cnt;
int A,C,G,T;
int exsys(int a,int c,int g,int t)
{
int BT=1;
int BG=BT*(T+1);
int BC=BG*(G+1);
int BA=BC*(C+1);
return a*BA+c*BC+g*BG+t;
}
inline int idx(char c)
{
if(c=='A') return 0;
else if(c=='C') return 1;
else if(c=='G') return 2;
else return 3;
}
void insert(char s[])
{
int len = strlen(s);
int u = 0;
for (int i = 0; i<len; i++)
{
int v = idx(s[i]);
if (!ch[u][v]) ch[u][v] = ++cnt;
u = ch[u][v];
}
val[u]++;
}
int dp[15000][505];
void slove(char s[])
{
int ANS=0;
int len=strlen(s);
A=0;C=0;G=0;T=0;
for(int i=0;i<len;i++)
if(s[i]=='A') A++;
else if(s[i]=='C') C++;
else if(s[i]=='G') G++;
else T++;
memset(dp,-1,sizeof(dp));
dp[0][0]=0;
for(int a=0;a<=A;a++)
for(int c=0;c<=C;c++)
for(int g=0;g<=G;g++)
for(int t=0;t<=T;t++)
for(int p=0;p<=cnt;p++)
{
int now=exsys(a,c,g,t);
if(dp[now][p]==-1) continue;
if(dp[now][p]>ANS) ANS=dp[now][p];
if(a<A)
{
int j=p;
int to=exsys(a+1,c,g,t);
int cc=idx('A');
while(j&&!ch[j][cc]) j=f[j];
j = ch[j][cc];
int ans=0;
if (val[j]) ans+=val[j];
int tmp = last[j];
while (tmp)
{
ans+=val[tmp];
tmp = last[tmp];
}
if(dp[now][p]+ans>dp[to][j]) dp[to][j]=dp[now][p]+ans;
}
if(t<T)
{
int j=p;
int to=exsys(a,c,g,t+1);
int cc=idx('T');
while(j&&!ch[j][cc]) j=f[j];
j = ch[j][cc];
int ans=0;
if (val[j]) ans+=val[j];
int tmp = last[j];
while (tmp)
{
ans+=val[tmp];
tmp = last[tmp];
}
if(dp[now][p]+ans>dp[to][j]) dp[to][j]=dp[now][p]+ans;
}
if(c<C)
{
int j=p;
int to=exsys(a,c+1,g,t);
int cc=idx('C');
while(j&&!ch[j][cc]) j=f[j];
j = ch[j][cc];
int ans=0;
if (val[j]) ans+=val[j];
int tmp = last[j];
while (tmp)
{
ans+=val[tmp];
tmp = last[tmp];
}
if(dp[now][p]+ans>dp[to][j]) dp[to][j]=dp[now][p]+ans;
}
if(g<G)
{
int j=p;
int to=exsys(a,c,g+1,t);
int cc=idx('G');
while(j&&!ch[j][cc]) j=f[j];
j = ch[j][cc];
int ans=0;
if (val[j]) ans+=val[j];
int tmp = last[j];
while (tmp)
{
ans+=val[tmp];
tmp = last[tmp];
}
if(dp[now][p]+ans>dp[to][j]) dp[to][j]=dp[now][p]+ans;
}
}
printf("%d\n",ANS);
}
void getFail()
{
queue<int>q;
f[0] = 0;
for (int c = 0; c<SIGMA_SIZE; c++)
{
int u = ch[0][c];
if (u)
{
f[u] = 0;
q.push(u);
last[u] = 0;
}
}
while (!q.empty())
{
int r = q.front();
q.pop();
for (int c = 0; c<SIGMA_SIZE; c++)
{
int u = ch[r][c];
if (!u)
{
continue;
}
q.push(u);
int v = f[r];
while (v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
}
int main()
{
int n;
int ks=0;
while (scanf("%d", &n),n)
{
memset(ch, 0, sizeof(ch));
memset(val, 0, sizeof(val));
cnt = 0;
char s[100];
for (int i = 1; i <= n; i++)
{
scanf("%s", s);
insert(s);
}
getFail();
scanf("%s", s);
ks++;
printf("Case %d: ",ks);
slove(s);
}
return 0;
}