String statistics (board questions

# The meaning of problems

Maintains a collection of strings, comprising two operations:
. 1) "LX" insert a string X
2) "Q X" ask how many times a string appears in the set
n operation, each query has an output, a single line of output

# Explanations

Title stencil
operation array op
* corresponding to a single character op
op corresponding character string

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+10;
 4 int n;
 5 int trie[N][26],cnt[N],tot=1;
 6 inline void insert(char *str){
 7     int p=1;
 8     for(int i=0;str[i];i++){
 9         int ch=str[i]-'a';
10         if(!trie[p][ch])
11             trie[p][ch]=++tot;
12         p=trie[p][ch];
13     }
14     cnt[p]++;
15 }
16 inline int query(char *str){
17     int p=1;
18     for(int i=0;str[i];i++){
19         int ch=str[i]-'a';
20         if(!trie[p][ch])
21             return 0;
22         p=trie[p][ch];
23     }
24     return cnt[p];
25 }
26 int main(){
27     cin>>n;
28     while(n--){
29         char op[2];
30         char x[N];
31         cin>>op>>x;
32         if(*op=='I'){
33             insert(x);
34         }
35         else {
36             cout<<query(x)<<endl;
37         }
38     }
39 }

 

 

Guess you like

Origin www.cnblogs.com/hhyx/p/12528892.html