hdu1251 字典树
又是字典树。 水题做的有点多了啊 ……
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<string> #include<cstdlib> #include<algorithm> #include<iostream> #include<map> using namespace std; #define MAXN 1111111 struct node { int num; node *next[26]; }; node *root,tree[MAXN]; int tot=0; node* creat() { node* temp=&tree[tot++]; temp->num=0; for(int i=0;i<26;i++) temp->next[i]=NULL; return temp; } void insert(char *str) { node *location = root; for(int i=0;str[i]!='\0';i++) { int num = str[i] - 'a'; if(location->next[num] == NULL) { location->next[num] = creat(); } location = location->next[num]; location->num++; } } int find(char *str) { node *location = root; for(int i=0;str[i]!='\0';i++) { int num = str[i] - 'a'; if(location->next[num] == NULL) return 0; location = location->next[num]; } return location->num; } int main() { char s[12]; root=creat(); while(gets(s)&&s[0]!='\0') { insert(s); } while(gets(s)) { printf("%d\n",find(s)); } return 0; }