本文主要是介绍hdu 1800字典树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
简单的字典树实现
#include<iostream>
#include<string>using namespace std;int N, n, Max;
string str;
char ch;struct node{node* son[10]; int tag;int count;node(){tag = 1;count = 0;for(int i = 0; i < 10; i ++){son[i] = NULL;} }
};node* root = new node();void BuildTree(string str)
{int i;for(i = 0; i < str.length(); i ++){if(str[i] == '0') continue;else break;}node* p = root;for(; i < str.length(); i ++){int s = str[i]-'0';//cout<<s<<endl;if(p->son[s] == NULL) {p->son[s] = new node();}p = p->son[s];p->tag = s;}p->count++;if(p->count > Max) Max = p->count;
}void DeleteTree()
{root = new node();
}int main()
{while(cin >> n){Max = 0;DeleteTree();while(n --){cin >> str;BuildTree(str);}cout<<Max<<endl;}return 0;
}
这篇关于hdu 1800字典树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!