hdu1671Phone List(字典树---判断有无相同的前缀单词)
2013-08-07 20:09
393 查看
1.题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1671
2.参考代码:
http://acm.hdu.edu.cn/showproblem.php?pid=1671
2.参考代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; struct node{ int cnt; node* next[10]; node(){ cnt=0; memset(next,0,sizeof(next)); } }; node* root=new node; void build(char* s){ node* p=root; int len=strlen(s); for(int i=0;i<len;i++) { if(!p->next[s[i]-'0']) p->next[s[i]-'0']=new node; p=p->next[s[i]-'0']; p->cnt++; } } int find(char* s){ node* p=root; int len=strlen(s); for(int i=0;i<len;i++) p=p->next[s[i]-'0']; return p->cnt; } void freedom(node* p){ for(int i=0;i<10;i++) { if(p->next[i]) freedom(p->next[i]); } free(p); } int main() { int t,n,i; char str[10001][30]; scanf("%d",&t); while(t--) { root=new node; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s",str[i]); build(str[i]); } int flag=0; for(i=0;i<n;i++) { if(find(str[i])!=1) ///一旦有一个访问超过一次就说明不可能 { flag=0; break; } flag=1; } if(flag) printf("YES\n"); else printf("NO\n"); freedom(root); } return 0; }
相关文章推荐
- hdu1247Hat’s Words(字典树---判断单词有无组合现象)
- poj--3630--Phone List(字典树+前缀判断)
- hdoj Immediate Decodability 1305 (字典树判断前缀)
- poj--3630--Phone List(字典树+前缀判断)
- POJ---2001 Shortest Prefixes[字典树---判断唯一前缀]
- HDU-单词的前缀 字典树
- 判断俩个整型数组有无相同的数
- POJ 1056 IMMEDIATE DECODABILITY(字典树,判断有没有一个是另一个的前缀)
- 利用字典树查找唯一标记某单词的最小前缀
- 单词的前缀 字典树
- 如何返回一个字符串中多个单词相同的前缀
- Immediate Decodability(字典树-判断是否存在前缀)
- 判断字符串中有无相同字符
- poj 3630 Phone List 【字典树】【判断一个字符串集里面 是否存在一个字符串是另一个字符串前缀】
- hdu 1671(字典树判断前缀)
- 字典树练习(一)hihocoder 1014(求相同前缀的数目)
- POJ 3630 字典树 判断单词是否不覆盖
- hdu Phone List 字典树的前缀判断
- poj Phone List 3630 (字典树 判断前缀)
- poj 2001/1056 Trie树(求单词表的最短前缀/判断立即码)