hdu 1800字典树

来源:互联网 发布:网络自制综艺节目研究 编辑:程序博客网 时间:2024/05/05 05:31

简单的字典树实现

#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;} 


0 0
原创粉丝点击