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