zoj_2876 Phone List
来源:互联网 发布:淘宝图片空间满了 编辑:程序博客网 时间:2024/06/10 18:07
题目链接:zoj_2876 Phone List
本题是要考察一个电话号码是否是另一个号码的前缀。因为数据量大,如果逐一 比较会超时。
对于超时问题,一般考虑两个因素:
1.算法过于低效
2.时间开销在频繁的输入输出上
Tip1:由于这里判断的是前缀,所以只需将所有电话号码从小到大排列,对于每个号码,前面的都不是后面的前缀,那么这组测试案例就符合要求。
Tip2:为了减少时间开销,可采用scanf和printf进行输入输出,记得要include<cstdio>
代码:
#include <iostream>#include <cstdio>#include <vector>#include <string>#include <algorithm>using namespace std;int main(){ vector<string> v; char ch[10]; string s; int n; int i,j; cin>>n; while(cin>>n) { v.clear(); for(i=0;i<n;i++) { scanf("%s",&ch);//切记这里第二个参数前的&,是从ch的地址读入字符 s=ch; v.push_back(s); } sort(v.begin(),v.end()); for(i=0;i<n-1;i++) { if(v[i+1].find(v[i])==0)//判断前一个号码是否是后一个的前缀 { printf("NO\n"); goto RL; } } printf("YES\n"); RL:continue;//继续while循环 }}
0 0
- zoj_2876 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
- Phone List
- 关于Intent的其它东西
- 第2周项目长方柱类
- Android开发_Handler
- const的用法,特别是用在函数后面
- lib 合并 打包 遇到 .o 重名的解决办法
- zoj_2876 Phone List
- 轻松搞定面试中的链表的题
- Android布局中使用weight的技巧(点名小软件的实现)
- Struts2下载、安装及开发流程
- Java UDP Socket
- android 中px dp sp 及资源目录drawable-hdpi drawable-ldpi drawable-mdpi等的分析
- nodejs之处理POST请求
- 大雨后的蓝天
- 最常用的 Eclipse 快捷键总结