trie树 HDU1671

来源:互联网 发布:suse查看开放端口号 编辑:程序博客网 时间:2024/06/16 12:59
#include <iostream>using namespace std;struct trie{trie():end(false){for(int i =0; i < 10; ++i)next[i] = NULL;}bool end;trie *next[10];};void insert(trie *root, const char *a){trie *p = root;int len = strlen(a);for(int i = 0; i < len; ++i){if(!p->next[a[i] - '0']){trie *t = new trie;p->next[a[i] - '0'] = t;}p = p->next[a[i] - '0'];}p->end = true;}bool search(trie *root, const char *a){trie *p = root;int len = strlen(a);for(int i = 0; i < len; ++i){int k = a[i] - '0';p = p->next[k];if(p->end){if(i + 1 != len)return true;}}return false;}void del(trie *p){if(!p)return;for(int i = 0; i < 10; ++i){if(p->next[i]){del(p->next[i]);}}delete p;}int main(void){//freopen("1.txt", "r", stdin);int t, n;bool fail;cin >> t;while(t--){trie *root = new trie;fail = false;char celnum[10010][11];cin >> n;for(int i = 0; i < n; ++i){scanf("%s", celnum[i]);insert(root, celnum[i]);}for(int j = 0; j < n; ++j){if(search(root, celnum[j]))fail = true;}if(fail)cout << "NO" << endl;elsecout << "YES" << endl;del(root);}return 0;}


0 0
原创粉丝点击