huu 1251

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <utility>
 7 #include <vector>
 8 #include <map>
 9 #include <queue>
10 #include <stack>
11 #include <cstdlib>
12 #include <deque>
13 #include <set>
14 typedef long long ll;
15 #define lowbit(x) (x&(-x))
16 #define ls l,m,rt<<1
17 #define rs m+1,r,rt<<1|1
18 using namespace std;
19 const  int N=1e6+3;
20 int tree[N][30];
21 int num[N];
22 char s[15];
23 int pos;
24 void insert(char s[]){
25     int root=0,i;
26     for(i=0;s[i];i++){
27         int x=s[i]-'a';
28         if(!tree[root][x]){
29             tree[root][x]=pos++;
30         }
31         root=tree[root][x];
32         num[root]++;
33     }
34 }
35 int  query(char s[]){
36     int i;
37     int root=0;
38     for(i=0;s[i];i++){
39         int x=s[i]-'a';
40         if(!tree[root][x]){
41             return  0;
42         }
43         root=tree[root][x];
44     }
45     return num[root];
46 }
47 int main()
48 {   
49    pos=1;
50    /*
51     memset(tree,0,sizeof(tree));
52     memset(num,0,sizeof(num));
53     */
54     //加上上面的会超内存
55     while(gets(s)){
56         if(s[0]==NULL)
57             break;
58             insert(s);     
59     }
60     while(gets(s)){
61         printf("%d\n",query(s));
62     }
63     return  0;
64 }

猜你喜欢

转载自www.cnblogs.com/tingtin/p/9336997.html
今日推荐