HDU 1238 Substrings(求公共正反向连续子串)
2015-09-24 21:17
253 查看
Substrings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8504 Accepted Submission(s): 3931
[align=left]Problem Description[/align]
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.
[align=left]Input[/align]
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the
number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.
[align=left]Output[/align]
There should be one line per test case containing the length of the largest string found.
[align=left]Sample Input[/align]
2 3 ABCD BCDFF BRCD 2 rose orchid
[align=left]Sample Output[/align]
2 2
[align=left]Author[/align]
Asia 2002, Tehran (Iran), Preliminary
//优化 #include <iostream> #include <cstdio> #include <string> #include <string.h> using namespace std; const int N=100+10; int n,id,re,ilen; char c ; int len ; int main(){ int t,i,small,j,k,z,zz; bool ok; scanf("%d",&t); while(t--){ re=0; small=N; scanf("%d",&n); for(i=0;i<n;i++){ scanf("%s",c[i]); len[i]=strlen(c[i]); if(len[i]<small) { small=len[i]; id=i; } } ilen=len[id]; for(i=ilen;re==0&&i>0;i--){ //枚举子串长度 for(j=0;re==0&&j+i-1<ilen;j++){ //枚举模板字串的起始点 for(k=0;k<n;k++){ if(k==id) continue; ok=0; for(z=0;z+i-1<len[k];z++) { //从左开始 for(zz=0;zz<i;zz++){ if(c[k][z+zz]!=c[id][j+zz]) break; } if(zz==i){ ok=1; break; } } for(z=len[k]-1;!ok&&z-i+1>=0;z--) { //从右开始 for(zz=0;zz<i;zz++){ if(c[k][z-zz]!=c[id][j+zz]) break; } if(zz==i){ ok=1; break; } } if(!ok) break; } if(k==n){ re=i; break; } } } printf("%d\n",re); } return 0; } /*哎呀 循环放错了 不过AC了 #include <iostream> #include <cstdio> #include <string> #include <string.h> #include <algorithm> using namespace std; const int N=100+10; int n,id,re,ilen,rlen; char c ; int len ; int main(){ int t,i,small,j,k,z,zz; bool ok; scanf("%d",&t); while(t--){ re=0; rlen=0; small=N; scanf("%d",&n); for(i=0;i<n;i++){ scanf("%s",c[i]); len[i]=strlen(c[i]); if(len[i]<small) { small=len[i]; id=i; } } ilen=len[id]; for(i=0;i<ilen;i++){ //枚举模板字符串的字符起始位置 for(j=ilen-i;i+j-1<ilen&&j>0;j--) { //子串的长度 for(k=0;k<n;k++){ //枚举每个字符串 if(k==id) continue; ok=0; for(z=0;z+j-1<len[k];z++){ //左开始的位置 for(zz=0;zz<j;zz++) //每个字符比较 { if(c[k][z+zz]!=c[id][i+zz]) break; } if(zz==j) { ok=1; break; } } for(z=len[k]-1;!ok&&z-j+1>=0;z--){ //从右开始 for(zz=0;zz<j;zz++) { if(c[k][z-zz]!=c[id][i+zz]) break; } if(zz==j) { ok=1; break; } } if(!ok) //有一个不行 这个子串就不行了 break; } if(k==n) { //坑了好久 枚举的起点放在长度外面了 造成被更新了...日了狗了 re=max(re,j); break; } } } printf("%d\n",re); } return 0; } */
相关文章推荐
- Scala教程(十)函数与闭包详解
- Java中Collection和Collections的区别
- VS2010/MFC编程之树型控件的创建及CTreeCtrl类的成员函数和实例
- IOS开发基础知识--碎片22
- 3、下载与安装Eclipse
- [转]Android ListView最佳处理方式,ListView拖动防重复数据显示,单击响应子控件
- HDU - 3496 Watch The Movie(01背包)
- 二级指针的申请跟删除(基于C++的new机制)
- HttpClient使用详解
- [转]Android学习笔记:TabHost 和 FragmentTabHost
- 【Android】loadData与loadDataWithBaseURL的区别
- [sicily]1157. The hardest problem
- CF 579A (二进制拆分)
- 使用Application实现Fragment之间的传值
- 关于mybatis中基本类型条件判断问题
- Windows 7 and Ubuntu install issues
- 题目:利用递归求1+2+3...100的和
- 杭电2066-一个人的旅行(最短路径)
- 让你彻底理解hashCode的作用
- Insertion Sort List