POJ2945:Find the Clones——题解
http://poj.org/problem?id=2945
还是trie树……对于结束标记累加并且开个数组记录一下即可。
#include<cstdio> #include<cstring> using namespace std; struct node{ int ed; int son[4]; void clear(){ memset(son,0,sizeof(son)); ed=0; } }tree[500050]; inline int turn(char ch){ if(ch=='A')return 0; if(ch=='C')return 1; if(ch=='G')return 2; return 3; } char s[20050][25]; int t[20001]; int tot; int n,m; void insert(int k){ int l=m; int now=1; for(int i=0;i<l;i++){ if(tree[now].son[turn(s[k][i])]==0){ tot++; tree[now].son[turn(s[k][i])]=tot; } now=tree[now].son[turn(s[k][i])]; } t[tree[now].ed]--; tree[now].ed++; t[tree[now].ed]++; return; } int main(){ while(scanf("%d%d",&n,&m)!=EOF&&(m!=n&&n!=0)){ for(int i=1;i<=500049;i++)tree[i].clear(); tot=1; memset(t,0,sizeof(t)); for(int i=1;i<=n;i++){ char ch=0;int j=0; while(ch<'A'||ch>'Z')ch=getchar(); while(ch>='A'&&ch<='Z'){s[i][j]=ch;ch=getchar();j++;} insert(i); } for(int i=1;i<=n;i++){ printf("%d\n",t[i]); } } return 0; }