#include <cstring>
#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
class Trie{
private :
map<char,Trie *> * root;
pair<bool,int> info;
inline Trie * makeNext(char c){
if(root == NULL){
root = new map<char ,Trie * >;
}
map<char, Trie *>::iterator it = root->find(c);
Trie * in ;
if( it == root->end()){
in = new Trie();
root->insert( pair<char , Trie * >(c,in));
}else{
in = it->second;
}
info.second ++ ;
return in;
}
inline void makeEnd(){
info.second ++ ;
info.first = true;
}
inline Trie * getChild(char c){
//该节点后面什么都没有了
if( root == NULL ){
return NULL;
}
map<char, Trie *>::iterator it = root->find(c);
if(it == root->end()){
//有兄弟节点,但是没有这个后续
return NULL;
}
return it->second;
}
void destory(){
if(root != NULL){
for(map<char ,Trie *> :: iterator it = root->begin() ; it!=root->end() ; ++ it ){
it->second->destory();
}
delete root;
}
}
public :
static pair<bool,int> None ;
Trie(){
root = NULL;
info = pair<bool,int>(false,);
}
~Trie(){
destory();
}
void addStr(char * str){
int len = strlen(str);
Trie * nowRoot = this;
while( (*str)!='\0'){
nowRoot = nowRoot->makeNext(*str);
++str;
}
nowRoot->makeEnd();
} pair<bool,int> findStr(char * str){
Trie * nowRoot = this;
while((*str)!='\0'){
nowRoot = nowRoot->getChild(*str);
if( nowRoot == NULL){
return None;
}
str++;
}
return nowRoot -> info;
}
};
pair<bool,int> Trie :: None = pair<bool,int>(false,-);
int main(){
Trie * a = new Trie();
a->addStr("abcdfg");
a->addStr("abcd");
a->addStr("abc");
a->addStr("ab");
cout<<"stop "<<endl;
pair <bool,int> ans = a->findStr("abc");
cout<< ans.first << " " << ans.second<<endl;
delete a;
return ;
}