标签:访问 search trie turn cin pre const ret 前缀
1 const int maxn = 4e5 + 5; 2 int trie[maxn][26]; 3 int color[maxn]; 4 int vis[maxn]; 5 int k = 1; 6 7 void insert(string w){ 8 int len = w.size(); 9 int p = 0; 10 for (int i = 0; i < len; i++) { 11 int c = w[i] - ‘a‘; 12 if (!trie[p][c]) { 13 trie[p][c] = k; 14 k++; 15 } 16 p = trie[p][c]; 17 vis[p]++; 18 } 19 color[p] = 1; 20 } 21 22 int search(string s) { //寻找所有前缀为s的串数 23 int len = s.size(); 24 int p = 0; 25 for (int i = 0; i < len; i++) { 26 int c = s[i] - ‘a‘; 27 if (!trie[p][c]) 28 return 0; 29 p = trie[p][c]; 30 } 31 return vis[p]; 32 } 33 34 int main() 35 { 36 int t, q; //总串数 访问数 37 string s; 38 cin >> t >> q; 39 while (t--) { 40 cin >> s; 41 insert(s); 42 } 43 while (q--) { 44 cin >> s; 45 printf("%d\n", search(s)); 46 } 47 return 0; 48 }
标签:访问 search trie turn cin pre const ret 前缀
原文地址:https://www.cnblogs.com/Kiana-/p/12268268.html