Statistics prefix

Description
is given a lot of string (only lowercase letters) and a number of questions strings, the statistics of the number of strings to a string of questions prefix (the word itself is its own prefix).
The Input
Input n, n expressed characters string (n <= 10000)
Next n lines, each line a string of not more than 10 degrees
inputs m, there are m represents question (m <= 100)
the second part is a series of questions, each question a line each question is a string.
the Output
for each question, a question is given in the prefix string number.
the Sample the Input
. 5
Banana
Band
Bee
Absolute
ACM
. 4
BA
B
Band
ABC
the Sample the Output
2
. 3
. 1
0

sol: The method with the Phone list, referred to the number of points occurring at each node.

 1 #include<cstring>
 2 #include<iostream>
 3 using namespace std;
 4 int a[100001][26],tot=1,End[100001];
 5 void ins(char *str)
 6 {
 7     int n=strlen(str),p=1;
 8     for(int i=0;i<n;i++)
 9     {
10         int l=str[i]-'a';
11         IF (! A [p] [L])
 12 is              A [p] [L] ++ = TOT;
 13 is          p = A [p] [L];
 14          End [p] ++; // statistical occurrence node p number 
15      }
 16  }
 . 17  int Find ( char * STR)
 18 is  {
 . 19      int n-= strlen (STR), P = . 1 ;
 20 is      for ( int I = 0 ; I <n-; I ++ )
 21 is      {
 22 is          int L = STR [I] - ' A ' ;
 23 is         p=a[p][l];
24         if(!p)return 0;
25     }
26     return End[p];
27 }
28 char str[11];
29 int main()
30 {
31     int n,m;
32     scanf("%d",&n);
33     for(int i=1;i<=n;i++)
34     {
35         scanf("%s",str);
36         ins(str);
37     }
38     scanf("%d",&m);
39     for(int i=1;i<=m;i++)
40     {
41         scanf("%s",str);
42         printf("%d\n",find(str));
43     }
44     return 0;
45 }

 

Guess you like

Origin www.cnblogs.com/cutepota/p/12589734.html