Phone List
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
4
-
描述
-
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.
-
输入
- The first line of input gives a single integer, 1 ≤ t ≤ 10, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 100000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits. 输出
- For each test case, output "YES" if the list is consistent, or "NO" otherwise. 样例输入
-
2 3 911 97625999 91125426 5 113 12340 123440 12345 98346
样例输出
-
NO YES
来源
- POJ 上传者
- iphxer
思路:
从大往小将字符串加入字典树,,如果将这个字符串都加进去没有增加新的字典树分支就说明这个字符串是以前字符串的前缀。。就可以输出NO了
代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n;
struct node{
char hh[10];
int ll;
}ch[100100];
struct trie{
struct trie *hai[10];
};
struct trie root;
bool cmp(node xx,node yy)
{
if (xx.ll!=yy.ll)
return xx.ll>yy.ll;
for (int i=0;i<xx.ll;i++)
{
if (i==xx.ll-1)
return xx.hh[i]>yy.hh[i];
else
{
if (xx.hh[i]!=yy.hh[i])
return xx.hh[i]>yy.hh[i];
}
}
}
int jia(char xx[])
{
struct trie *kk;
kk=&root;
int ll=strlen(xx);
for (int i=0;i<ll;i++)
{
if (kk->hai[xx[i]-'0'])
{
if (i==ll-1)
return true;
}
else
{
kk->hai[xx[i]-'0']=new trie;
memset(kk->hai[xx[i]-'0'],0,sizeof(trie));
}
kk=kk->hai[xx[i]-'0'];
}
return false;
}
void Freedom(trie* p) //释放new的内存。。。。看别人博客说这是一种态度.-。-
{
int i;
for(i=0;i<10;i++)
{
if(p->hai[i]!=NULL)
Freedom(p->hai[i]);
}
delete p;
}
int main()
{
int t;scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%s",ch[i].hh);
ch[i].ll=strlen(ch[i].hh);
}
sort(ch,ch+n,cmp);
for (int i=0;i<10;i++)
root.hai[i]=0;
bool fafe=true;
for (int i=0;i<n;i++)
{
if (jia(ch[i].hh))
{
fafe=false;
break;
}
}
if (fafe)
printf("YES\n");
else
printf("NO\n");
/* printf("guo\n");
for (int i=0;i<10;i++)
Freedom(root.hai[i]);//这一点好像有问题。。。。
printf("guo\n");*/
}
return 0;
}
动态建树:需释放内存
带释放内存:内存突然少了几倍-.-好开心。。。。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n;
struct node{
char hh[10];
int ll;
}ch[101000];
struct trie{
struct trie *hai[10];
};
struct trie *root;
bool cmp(node xx,node yy)
{
if (xx.ll!=yy.ll)
return xx.ll>yy.ll;
for (int i=0;i<xx.ll;i++)
{
if (i==xx.ll-1)
return xx.hh[i]>yy.hh[i];
else
{
if (xx.hh[i]!=yy.hh[i])
return xx.hh[i]>yy.hh[i];
}
}
}
int jia(char xx[])
{
struct trie *kk;
kk=root;
int ll=strlen(xx);
for (int i=0;i<ll;i++)
{
if (kk->hai[xx[i]-'0'])
{
if (i==ll-1)
return true;
}
else
{
kk->hai[xx[i]-'0']=new trie;
memset(kk->hai[xx[i]-'0'],0,sizeof(trie));
}
kk=kk->hai[xx[i]-'0'];
}
return false;
}
void Freedom(trie* p) //释放new的内存。。。。看别人博客说这是一种态度.-。-
{
int i;
for(i=0;i<10;i++)
{
if(p->hai[i]!=NULL)
Freedom(p->hai[i]);
}
delete p;
}
int main()
{
int t;scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%s",ch[i].hh);
ch[i].ll=strlen(ch[i].hh);
}
sort(ch,ch+n,cmp);
root=new trie();
memset(root,0,sizeof(trie));
bool fafe=true;
for (int i=0;i<n;i++)
{
if (jia(ch[i].hh))
{
fafe=false;
break;
}
}
if (fafe)
printf("YES\n");
else
printf("NO\n");
Freedom(root);
}
return 0;
}
静态建树:
开始已分配内存单元-.-时间上快-.-
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,wei;
struct node{
char hh[10];
int ll;
}ch[101000];
struct trie{
struct trie *hai[10];
}di[1001000];
struct trie *root;
bool cmp(node xx,node yy)
{
if (xx.ll!=yy.ll)
return xx.ll>yy.ll;
for (int i=0;i<xx.ll;i++)
{
if (i==xx.ll-1)
return xx.hh[i]>yy.hh[i];
else
{
if (xx.hh[i]!=yy.hh[i])
return xx.hh[i]>yy.hh[i];
}
}
}
int jia(char xx[])
{
struct trie *kk;
kk=root;
int ll=strlen(xx);
for (int i=0;i<ll;i++)
{
if (kk->hai[xx[i]-'0'])
{
if (i==ll-1)
return true;
}
else
{
kk->hai[xx[i]-'0']=&di[wei++];
memset(kk->hai[xx[i]-'0'],0,sizeof(trie));
}
kk=kk->hai[xx[i]-'0'];
}
return false;
}
int main()
{
int t;scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%s",ch[i].hh);
ch[i].ll=strlen(ch[i].hh);
}
sort(ch,ch+n,cmp);
wei=0;
root=&di[wei++];
memset(root,0,sizeof(trie));
bool fafe=true;
for (int i=0;i<n;i++)
{
if (jia(ch[i].hh))
{
fafe=false;
break;
}
}
if (fafe)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}