5 green red blue red red 3 pink orange pink 0
red pink
<span style="font-size:14px;">#include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<stack> #include<queue> #include<vector> #include<algorithm> using namespace std; #define INF 0x7fffffff;</span>
<span style="font-size:14px;">//字典树:
<strong><span style="color:#ff0000;">struct node
{
int x;
node *next[26];
node()
{
x=1;
memset(next,0,sizeof(next));
}
};
node *root=NULL;
void insert(char *s)
{
node *p=root;
int i,k;
for(i=0;i<strlen(s);i++)
{
k=s[i]-'a';
if(p->next[k]==NULL)
p->next[k]=new node;
else
p->next[k]->x++;
p=p->next[k];
}
}
int search(char *s)
{
node *p=root;
int i,k;
for(i=0;i<strlen(s);i++)
{
k=s[i]-'a';
if(p->next[k]==NULL)
return 0;
p=p->next[k];
}
return p->x;
}</span></strong>
char str1[50],str2[50];
int main()
{
<strong><span style="color:#ff0000;">root=new node;//注意之前一定要建树</span></strong>
while(gets(str1)&&strlen(str1))//注意此处的输入控制
insert(str1);
while(gets(str2)!=NULL)
{
int temp=search(str2);
printf("%d\n",temp);
}
return 0;
}</span>原文地址:http://blog.csdn.net/lh__huahuan/article/details/45111307