400字范文,内容丰富有趣,生活中的好帮手!
400字范文 > Phone List 南邮NOJ 1522

Phone List 南邮NOJ 1522

时间:2023-10-21 19:00:27

相关推荐

Phone List 南邮NOJ 1522

Phone List

时间限制(普通/Java): 1000 MS/3000 MS 运行内存限制 : 65536 KByte

总提交 : 108测试通过 : 41

题目描述

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≤40, the number of test cases. Each

test case starts withn, the number of phone numbers, on a separate line, 1≤n≤10000.

Then followsnlines 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

提示

undefined

本题求解未使用更优化的数据结构~

仅仅使用了string类中一些函数,比如strncmp等,我觉得这种解法反而更好些【渣渣捂脸哭~】

思路就是:首先定义二维数组存储Phone List,然后进行qsort排序,这个排序很巧妙的把电话表按照数字对应的ASCII码进行从大到小、从短到长的排序,我们接下来就根据排序结果进行循环判断,如果前后两个字符串啊a[i],a[i+1]中,a[i+1[的前部分包含a[i]中的所有字符,那么就可以得出结论,该电话表不一致(not consistent)。这个判断通过strncmp(a[i],a[i+1],strlen(a[i])==0来实现。

下面给出AC代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int comp(const void *a,const void *b){return strcmp((char*)a,(char*)b);}int T;char phonelist[10000][11];int main(){scanf("%d",&T); //测试样例个数while(T--){int n; //电话表个数scanf("%d",&n);for(int i=0;i<n;i++){scanf("%s",phonelist[i]);}qsort(phonelist,n,sizeof(phonelist[0]),comp);bool flag=false;for(int i=0;i<n-1;i++){if(strncmp(phonelist[i],phonelist[i+1],strlen(phonelist[i]))==0){flag=true;break;}}if(flag==true){printf("NO\n");}else{printf("YES\n");}}return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。