//#pragma comment(linker, "/STACK:102400000") #include<cstdlib> #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<set> #include<map> #include<list> #include<queue> #include<stack> #include<vector> #define tree int o,int l,int r #define lson o<<1,l,mid #define rson o<<1|1,mid+1,r #define lo o<<1 #define ro o<<1|1 #define pb push_back #define mp make_pair #define ULL unsigned long long #define LL long long #define inf 0x3fffffff//不要太大!溢出! #define eps 1e-7 #define N 1009 #define M 4 #define mod 100000 using namespace std; int m,n,T,t,x,y,u; int ch[N][4]; int v[N]; int f[N],last[N],num; char str[N]; int d[N][N]; void clear()//Trie树初始化 { memset(d,-1,sizeof(d)); num=1; memset(ch[0],0,sizeof(ch[0])); memset(v,0,sizeof(v)); memset(last,0,sizeof(last)); } int idx(char c) { switch(c) { case 'A': return 0; case 'C': return 1; case 'G': return 2; case 'T': return 3; } return 0; } void insert(char str[],int value)//建Trie树 { int len=strlen(str); int u=0; for (int i=0; i<len; ++i ) { int c=idx(str[i]); if(!ch[u][c])//保存的是结点坐标 { memset(ch[num],0,sizeof(ch[num])); ch[u][c]=num++;// } u=ch[u][c]; } v[u]=value; } void getac() { queue<int> q;//保存的节点下标 f[0]=0; for (int c=0; c<M; ++c ) { int u=ch[0][c]; if(u)//不需要优化的else { q.push(u); f[u]=0; last[u]=v[u];//WA,可能有长度为1的串 } } while(!q.empty()) { int r=q.front(); q.pop(); for (int c=0; c<M; ++c ) { int u=ch[r][c]; if(u) { q.push(u); int s=f[r]; f[u]=ch[s][c]; last[u]=(v[u]||last[f[u]]);//改 } else //重要优化 ch[r][c]=ch[f[r]][c]; } } } int dp(int u,int k) { if(k==n)return 0; int &ans=d[u][k]; if(ans!=-1)return ans; ans=inf; for(int i=0; i<4; i++) { int c=ch[u][i]; if(last[c]==0) { ans=min(ans,dp(c,k+1)+(idx(str[k])!=i?1:0)); } } return ans; } int main() { #ifndef ONLINE_JUDGE freopen("ex.in","r",stdin); #endif int ncase=0; // scanf("%d",&T); // while(T--) while(scanf("%d",&m)==1&&m) { clear(); while(m--) { scanf("%s",str); insert(str,1); } getac(); scanf("%s",str); n=strlen(str); printf("Case %d: %d ",++ncase,dp(0,0)==inf?-1:dp(0,0)); } return 0; }