hdu 1251
来源:互联网 发布:软件项目管理软件书籍 编辑:程序博客网 时间:2024/06/05 19:39
#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct node{ node* next[28]; int count; node():count(0){memset(next,0,sizeof(next));}};node *root=new node;void Insert(char *str){ int i; node *t=root; int l=strlen(str); for(i=0;i<l;i++) { if(t->next[str[i]-'a']==0) { node *p=new node; t->next[str[i]-'a']=p; p->count++; t=t->next[str[i]-'a']; } else { t->next[str[i]-'a']->count++; t=t->next[str[i]-'a']; } }}void Find(char *str){ int i,l=strlen(str); node *t=root; for(i=0;i<l;i++) { t=t->next[str[i]-'a']; if(t==0) { printf("0\n"); return ; } } printf("%d\n",t->count);}int main(){ char str[15]; while(gets(str)) { if(str[0]=='\0') break; else Insert(str); } while(gets(str)) { if(str[0]=='\0') break; else Find(str); } return 0;}