POJ 3080 Blue Jeans(水~)
2015-08-29 08:33
591 查看
Description
求m个长度为60的字符串的最长连续公共子串(2<=m<=10),规定:
1,最长公共串长度小于3不输出
2,若出现等长的最长的子串,则输出字典序最小的串
Input
多组输入,第一行为用例组数T,每组用例第一行为一整数m表示字符串个数,之后m行每行一个长度为60的字符串
Output
输出最长连续公共子串,最长公共串长度小于3输出no significant commonalities,若出现等长的最长的子串,则输出字典序最小的串
Sample Input
Sample Output
no significant commonalities
AGATAC
CATCATCAT
Solution
暴力枚举公共子串长度即可
Code
求m个长度为60的字符串的最长连续公共子串(2<=m<=10),规定:
1,最长公共串长度小于3不输出
2,若出现等长的最长的子串,则输出字典序最小的串
Input
多组输入,第一行为用例组数T,每组用例第一行为一整数m表示字符串个数,之后m行每行一个长度为60的字符串
Output
输出最长连续公共子串,最长公共串长度小于3输出no significant commonalities,若出现等长的最长的子串,则输出字典序最小的串
Sample Input
Sample Output
no significant commonalities
AGATAC
CATCATCAT
Solution
暴力枚举公共子串长度即可
Code
#include<cstdio> #include<cstring> #include<iostream> using namespace std; #define maxn 65 char p[maxn][maxn]; char ans[maxn]; int T,m; int main() { scanf("%d",&T); while(T--) { scanf("%d",&m); for(int i=0;i<m;i++) scanf("%s",p[i]); int len=0; for(int i=0;i<strlen(p[0]);i++) for(int j=i+2;j<strlen(p[0]);j++) { char s[maxn]; strncpy(s,p[0]+i,j-i+1); s[j-i+1]='\0'; bool flag=true; for(int k=1;flag&&k<m;k++) if(strstr(p[k],s)==NULL) flag=false; if(flag&&(j-i+1>len||(j-i+1==len&&strcmp(ans,s)>0)))//更新最长长度和子串 { len=j-i+1; strcpy(ans,s); } } if(len<3)//长度小于3则不输出 printf("no significant commonalities\n"); else printf("%s\n",ans); } return 0; }
相关文章推荐
- 单行文本框和多行文本框UiTextfield UiTextView
- POJ 2871 A Simple Question of Chemistry(水~)
- POJ 2785 4 Values whose Sum is 0(hash表)
- 从数据库中提取到ueditor并且重新编辑
- POJ 2664 && HDU 1144 Prerequisites?(水~)
- 【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】
- 6、iOS 开发之 UIPickerView
- 4、iOS 开发之 UITableView
- 3、iOS 开发之 UIScrollView UIPageController NSTimer
- iOS部分-UI基础控件 - 01天 入门 第11课 上下左右移动
- 新建Android工程报错:'Building workspace' has encountered a problem...解决办法
- ngui自适应的一点学习
- ios开发-UI-自定义Tabbar 图书布局
- iOS之Operation Queues 和 Grand Central Dispatch
- PHP生成UUID
- .NET 的 Debug 和 Release build 对执行速度的影响
- 关于CodeFirst异常:无法确定类型'XXX'和类型‘YYY’之间的关联的主体端,必须使用关系 Fluent API 或数据注释显式配置此关联的主体端。
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记44 Popover Segue
- Android UI设计:Notification
- Android UI设计:PopupWindow