Trie树(字典树)poj3630+hdu1671
来源:互联网 发布:h5免费制作网站 知乎 编辑:程序博客网 时间:2024/06/05 16:09
Description
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:
- Emergency 911
- Alice 97 625 999
- Bob 91 12 54 26
In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.
Input
The first line of input gives a single integer, 1 ≤ t ≤ 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
Output
For each test case, output "YES" if the list is consistent, or "NO" otherwise.
Sample Input
2391197625999911254265113123401234401234598346
Sample Output
NOYES题意:判读是不是存在有的串是其他串的前缀。
思路:字典树的应用。
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=10010;int n,flag;char s[maxn];struct TRIE{ int sz; int ch[maxn*100*10][10]; int val[maxn*1000]; void clear(){memset(ch[0],0,sizeof(ch[0]));memset(val,-1,sizeof(val));sz=1;} int idx(char x){return x-'0';} void insert(char *s,int id) { int n=strlen(s); int u=0; for(int i=0;i<n;i++) { int c=idx(s[i]); bool is_new=false; if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[u][c]=sz++; is_new=true; } u=ch[u][c]; if(val[u]>0){flag=false;return ;} if(i==n-1&&!is_new){flag=false;return ;} } val[u]=id; }}tree;int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); flag=1; tree.clear(); for(int i=1;i<=n;i++) { scanf("%s",s); if(flag)tree.insert(s,i); } if(flag)printf("YES\n"); else printf("NO\n"); } return 0;}
hdu上这样会超内存,要用指针,还得释放内存
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=10010;int n,flag;char s[maxn];struct node{ node *next[26]; int val; node(){memset(next,0,sizeof(next));val=0;}};struct TRIE{ node *root; void build(){root=new node();} void clear(node *p) { for(int i=0;i<26;i++) { if(p->next[i]!=NULL) clear(p->next[i]); } delete p; } int idx(char x){return x-'0';} void insert(char *s,int id) { int n=strlen(s); node *p=root; for(int i=0;i<n;i++) { int c=idx(s[i]); bool is_new=false; if(!p->next[c]) { p->next[c]=new node(); is_new=true; } p=p->next[c]; if(p->val>0){flag=false;return ;} if(i==n-1&&!is_new){flag=false;return ;} } p->val=id; }}tree;int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); flag=1; tree.build(); for(int i=1;i<=n;i++) { scanf("%s",s); if(flag)tree.insert(s,i); } if(flag)printf("YES\n"); else printf("NO\n"); tree.clear(tree.root); } return 0;}
- Trie树(字典树)poj3630+hdu1671
- HDU1671 poj3630 Phone List 字典树
- C++——字典树(Trie树)例题——Phone List(POJ3630)(HDU1671)
- hdu1671(trie树(字典树))
- ZOJ2876 POJ3630 HDU1671 Phone List,静态Trie树
- POJ3630(Trie树)
- POJ3630(Trie树)
- [复习][HDU1671]字典树(trie树)phone list
- hdu1671( 字典树)
- POJ3630(Trie树)
- Trie树 poj3630
- HDU1671 POJ3630 Phone List Tire树
- trie树 HDU1671
- hdu1671 trie树
- hdu1671 trie树
- hdu1671 PhoneList (字典树)
- Phone List(poj3630,简单trie树)
- POJ3630 trie字典树水题
- StringBuffer
- 【爱上Java8】Java8函数式方法的方法引用
- hdu1548BFS
- 基于统计语言模型的拼音输入法
- 利用正则表达式处理字符
- Trie树(字典树)poj3630+hdu1671
- 做个JAVA 8的调查?
- poj 3261 Milk Patterns
- C++模板
- windows环境下音频文件播放
- Android蓝牙开发:蓝牙小车上位机(开源)(第二版)
- HTML5+ plus.maps地图详解(官方文档)
- 资本运作下的腾讯和帝国梦想
- quartz实现定时任务调度