您的位置:首页 > 其它

hdu 1672Phone List&&poj 3630Phone List

2015-12-02 20:51 519 查看


Phone List

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 15379 Accepted Submission(s): 5175



Problem 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:

1. Emergency 911

2. Alice 97 625 999

3. 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

2
3
911
97625999
91125426
5
113
12340
123440
12345
98346


Sample Output

NO
YES



动态与静态差距:




动态空间代码(poj过不了):

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef struct TrieNode{
int data;
TrieNode *next[10];
TrieNode(){
data=0;
memset(next,0,sizeof(next));
}
};
TrieNode *root=NULL;
void Build(char *c){
TrieNode *p=root;
TrieNode *q=NULL;
int i,v,l=strlen(c);
for(i=0;i<l;i++)
{
v=c[i]-'0';
if(p->next[v]==NULL){
q=new TrieNode;
p->next[v]=q;
}
p=p->next[v];
p->data++;
}
}
int Find(char *c){
int i,v,l=strlen(c);
TrieNode *p=root;
for(i=0;i<l;i++)
{
v=c[i]-'0';
if(p->next[v]==NULL){
return 0;break;
}
else p=p->next[v];
}
return p->data;
}
void Delate(TrieNode *root){
for(int i=0;i<10;i++)
if(root->next[i])
Delate(root->next[i]);
delete(root);
}
int main()
{
int t,n,s,i,p;
char c[10001][15];
cin>>t;
while(t--)
{
s=0;
root=new TrieNode;
cin>>n;
for(i=1;i<=n;i++)
{
scanf("%s",c[i]);
Build(c[i]);
}
for(i=1;i<=n;i++)
{
p=Find(c[i]);
if(p==0)break;
else if(s<p)s=p;
}
if(s>1||p==0)printf("NO\n");
else printf("YES\n");
Delate(root);
}
return 0;
}

静态空间代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int data_TrieNode;
struct TrieNode{
int data;
TrieNode *next[10];
void init(){
data=0;
memset(next,0,sizeof(next));
}
}Tree[100001];
inline TrieNode *new_TrieNode(){
Tree[data_TrieNode].init();
return &Tree[data_TrieNode++];
}
TrieNode *root=NULL;
void Build(char *c){
TrieNode *p=root;
TrieNode *q=NULL;
int i,v,l=strlen(c);
for(i=0;i<l;i++)
{
v=c[i]-'0';
if(p->next[v]==NULL){
q=new_TrieNode();
p->next[v]=q;
}
p=p->next[v];
p->data++;
}
}
int Find(char *c){
int i,v,l=strlen(c);
TrieNode *p=root;
for(i=0;i<l;i++)
{
v=c[i]-'0';
if(p->next[v]==NULL){
return 0;break;
}
else p=p->next[v];
}
return p->data;
}
int main()
{
int t,n,s,i,p;
char c[10001][15];
cin>>t;
while(t--)
{
data_TrieNode=0;
s=0;
root=new_TrieNode();
cin>>n;
for(i=1;i<=n;i++)
{
scanf("%s",c[i]);
Build(c[i]);
}
for(i=1;i<=n;i++)
{
p=Find(c[i]);
if(p==0)break;
else if(s<p)s=p;
}
if(s>1||p==0)printf("NO\n");
else printf("YES\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: