题意就不用说辣,就是套用字典树的模板
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
#define read freopen("q.in","r",stdin)
#define maxn 10002
struct dictree
{
int n;
struct dictree *child[26];
};
dictree *root;
void init()
{
root=(struct dictree *)malloc(sizeof(struct dictree));
memset(root->child,0,sizeof(root->child));
}
void insert(char *s)
{
struct dictree *cur,*newn;
cur=root;
for(;*s;s++)
{
if(cur->child[*s-'a']!=0)
{
cur=cur->child[*s-'a'];
cur->n+=1;
}
else
{
newn=(struct dictree*)malloc(sizeof(struct dictree));
memset(newn->child,0,sizeof(newn->child));
cur->child[*s-'a']=newn;
cur=newn;
cur->n=1;
}
}
}
int find(char *s)
{
struct dictree *cur,*newn;
cur=root;
for(;*s;s++)
{
if(cur->child[*s-'a']!=0)
cur=cur->child[*s-'a'];
else return 0;
}
return cur->n;
}
int main()
{
char s[11];
int i;
init();
while(gets(s) && s[0]!='\0')insert(s);
while(gets(s))
cout<<find(s)<<endl;
}
参考资料:
杭电PPT: