使用说明:t[string]存的是string的个数,insert(string)可以访问string的下标
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
struct Node{
int index;
struct Node *next[27];
Node(){
index = 0;
memset(next,NULL,sizeof(next));
}
};
template<int MAXW>
class Trie{
public:
Trie(){index = 0;root = new Node;}
int & operator[](string str){
return H[insert(str)];
}
int insert(string str){
rot = root;
for(int i = 0;i < str.size();++i){
if(!rot->next[str[i] - 'a'])
rot->next[str[i] - 'a'] = new Node;
rot = rot->next[str[i] - 'a'];
if(i == str.size() - 1){
if(!rot->index) rot->index = ++index;
}
}
return rot->index;
}
private:
int index,H[MAXW];
struct Node *root,*rot;
};
Trie<10005> t;