http://acm.hdu.edu.cn/showproblem.php?pid=1251 #include<iostream> #include<algorithm> #include<stdio.h> using namespace std; struct node { int sum; node *next[26]; }; void buildtrietree(node *head, char s[]) { node *p=new node(); p=head; for(int i=0; s[i]; i++) { int k=s[i]-‘a‘; if(p->next[k]==0) p->next[k]=new node(); p=p->next[k]; p->sum++; } } int query(node *head, char s[]) { node *p=new node(); p=head; for(int i=0; s[i]; i++) { int k=s[i]-‘a‘; if(p->next[k]==0) return 0; p=p->next[k]; } return p->sum; } int main() { char s[12]; node *head=new node(); while(gets(s), s[0]) buildtrietree(head, s); while(cin >> s) printf("%d\n", query(head, s)); return 0; }
时间: 2024-10-15 15:43:47