1 /******************************************************* 2 题目: 统计难题 (hdu 1251) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 4 算法: 字典树 5 提示: 这题压要用c++提交,G++会超内存 6 *******************************************************/ 7 #include8 #include 9 #include 10 #include 11 using namespace std;12 char s[11];13 typedef struct Node 14 {15 Node *next[26];16 int cut;17 }Node;18 Node *root;19 void inser(char *s)20 {21 Node *p=root;22 for (int i=0;s[i];i++)23 {24 int x=s[i]-'a';25 if (p->next[x]==NULL)26 {27 p->next[x]=(Node *)malloc(sizeof(Node));28 p->next[x]->cut=0;29 for (int i=0;i<26;i++) p->next[x]->next[i]=NULL;30 }31 p=p->next[x];32 p->cut++;33 }34 }35 int Find(char *s)36 {37 Node *p=root;38 for (int i=0;s[i];i++)39 {40 int x=s[i]-'a';41 if (p->next[x]==NULL) return 0;42 p=p->next[x];43 }44 return p->cut;45 }46 int main()47 {48 root=new Node();49 while (gets(s))50 {51 if (strcmp(s,"")==0) break;52 else inser(s);53 }54 while (gets(s))55 {56 printf("%d\n",Find(s));57 }58 return 0;59 }