hdu 1251 统计难题

因为查询的前缀字典可能不含有,这点没考虑清楚就码了,太粗心了,因为case只有一个,很多初始化操作都不需要了。

 1 #include<cstdio>
 2 #include<cstring>
 3 const int maxnode=500000;
 4 int ch[maxnode][26];
 5 int val[maxnode];
 6 int sz;
 7 void insert(char *s)
 8 {
 9     int u=0,len=strlen(s);
10     for(int i=0;i<len;i++)
11     {
12         int c=s[i]-'a';
13         if(!ch[u][c])
14             ch[u][c]=sz++;
15         u=ch[u][c];
16         val[u]++;
17     }
18 }
19 int find(char *s)
20 {
21     int u=0,len=strlen(s);
22     for(int i=0;i<len;i++)
23     {
24         int c=s[i]-'a';
25         if(ch[u][c])
26         u=ch[u][c];
27         else
28         return 0;
29     }
30     return val[u];
31 }
32 int main()
33 {
34         char s[15];
35         sz=1;
36         while(gets(s)&&s[0])
37         {
38             insert(s);
39         }
40         while(scanf("%s",s)!=EOF)
41         {
42             printf("%d
",find(s));
43         }
44        return 0;
45 }
原文地址:https://www.cnblogs.com/sooflow/p/3282570.html