HDU 1671 Phone List (qsort字符串排序与strncmp的使用 /字典树)
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25724 Accepted Submission(s): 8593
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
题意:判断每个样例输入的电话号码中是否有号码是其它号码的前缀
法1、将输入的几串号码排序,然后判断是否有一串号码是否是后面一串号码的前缀即
可。
法2、建立字典树,查询到单词尾部并且访问节点访问次数>1(因为要去除本身的情
况),即可证明该单词是其他单词的前缀(切记释放内存);
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char s[10001][11];
int cmp(const void *m,const void *n)
{
return strcmp((char*) m,(char*) n);//n和m倒过来即为降序
}
int main()
{
int T,n,flag;
scanf("%d",&T);
while(T--)
{
flag=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",s[i]);
qsort(s,n,sizeof(s[0]),cmp);//升序排序
//sizeof(s[0])是一个元素的大小,所有元素大小都一样
for(int i=0;i<n;i++){
if(!strncmp(s[i],s[i+1],strlen(s[i]))){
flag=1;break;
}
}
/*int strcmp(const char *s1,const char * s2); 比较s1,s2二个字符串的大小.
int strncmp(char *str1, char *str2, int maxlen); 比较s1,s2二个字符串,
前maxlen字符的大小。当maxlen为s1,s2中字符串最长长度时,相当
于strcmp.*/
if(flag)
printf("NO\n" );
else printf("YES\n");
}
return 0;
}
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char s[11111][11];
struct node
{
int cnt;//记录节点访问次数
struct node *next[10];
node()
{
for(int i=0;i<=9;i++)
next[i]=NULL;
cnt=0;
}
}*rt;
void insert(char *ss)
{
int x;
node *p=rt;
node *tem=NULL;
for(int i=0;ss[i];i++)
{
x=ss[i]-'0';
if(p->next[x]==NULL)
tem=new node,p->next[x]=tem;
p=p->next[x];
p->cnt++;
}
}
bool check(node *p)
{
for(int i=0;i<10;i++)
if(p->next[i])
return 1;
return 0;
}
bool find1(char *ss)
{
int x;
node *p=rt;
for(int i=0;ss[i];i++)
{
x=ss[i]-'0';
if(!p->next[x])
return 0;
p=p->next[x];
}
if(p->cnt>1)return 1;//访问过不止一次即可证明情况存在
else return 0;
}
void del(node *root)
{
for(int i=0;i<10;i++)
{
if(root->next[i]!=NULL)
{
del(root->next[i]);
}
}
free(root);//释放
}
int main()
{
int T,n,flag;
scanf("%d",&T);
while(T--)
{
flag=0;
rt=new node;//必须在内部,否则超限
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",s[i]),insert(s[i]);
for(int i=0;i<n;i++)
if(find1(s[i])){
flag=1;break;
}
if(flag)
printf("NO\n");
else
printf("YES\n");
del(rt);//防止内存超限
}
return 0;
}