Phone List 南邮NOJ 1522
来源:互联网 发布:pyqt5 知乎编程 编辑:程序博客网 时间:2024/06/01 03:58
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 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.
输出
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;}
总提交 : 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 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.
输出
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;}
- Phone List 南邮NOJ 1522
- 南邮 OJ 1522 Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Phone List
- Socket编程笔记
- Ibatis 批量操作 和 三级下拉框连动问题
- Socket网络编程入门
- 矩阵(n阶方阵)的初等变换 初等矩阵 几何意义
- oracle_基础八(Undo)
- Phone List 南邮NOJ 1522
- 最傻的人是我
- 蓝桥杯那些事
- 算法导论 第18章 B 树
- 统计源代码文件的总行数、空行数、注释行数、代码行数
- 将一个二叉树镜像
- 【R for fun 系列】汉诺塔
- C++ 运算结果出现 1.#IND, 1.#INF nan, inf 原因
- 在Azure 环境里安装 SharePoint 2013 系统(三)在Windows Azure VM中配置Sql Server 2012