hdu 1251 统计难题
题解:
#include<iostream>
#include<cstdio>#include<algorithm>
#include<cstring>
using namespace std;
struct node
{
int count_;
node *trie[26];
}*root;
void init()
{
root=new node;
for(int i=0;i<26;i++) root->trie[i]=NULL;
}
void insert_(char *str)
{
int len=strlen(str);
node *head=root;
for(int i=0;i<len;i++)
{
int cur=(int)(str[i]-'a');
if(head->trie[cur]==NULL)
{
head->trie[cur]=new node;
head=head->trie[cur];
head->count_=1;
for(int j=0;j<26;j++) head->trie[j]=NULL;
}
else
{
head=head->trie[cur];
head->count_++;
}
}
}
int search_(char *que)
{
int len=strlen(que);
node *head=root;
for(int i=0;i<len;i++)
{
int cur=(int)(que[i]-'a');
if(head->trie[cur]==NULL)
return 0;
else
head=head->trie[cur];
}
return head->count_;
}
int main(void)
{
//freopen("c.txt","r",stdin);
char str[11],que[11];
init();
while(gets(str) && strcmp(str,"")!=0) insert_(str);
while(~scanf("%s",que)) cout<<search_(que)<<endl;
return 0;
}