敲出来字典树模板,用的双指针建的
#include <iostream> #include <string> #include <regex> using namespace std; const int N = 26; struct trieNode{ char c; trieNode** son; int cnt = 0; trieNode (char C){ cnt = 0; c=C; son = new trieNode *[N]; for(int i = 0;i < N;i++){ son[i] = nullptr; } }; }; trieNode *root; void insert(string s) { trieNode *p; p = root; for(int i = 0;i < s.size();i++){ int x = s[i] - 'a'; if(!p->son[x]) p->son[x] = new trieNode(s[i]); p = p->son[x]; } p->cnt++; } int query(string s) { trieNode *p; p = root; for(int i=0;i < s.size();i++){ if(p->son[s[i]-'a']== nullptr) return 0; p = p->son[s[i]-'a']; } return p->cnt; } int main() { root = new trieNode(' '); string s; for(int i=0;i<5;i++){ cin>>s; insert(s); } for(int i=0;i<3;i++){ cin>>s; cout<<query(s); } return 0; }
标签:cnt,int,++,son,trieNode,root,字典 From: https://www.cnblogs.com/wangzhelin/p/16875473.html