HDU 1238 Substrings(求公共正反向连续子串)
来源:互联网 发布:中科院数据共享平台 编辑:程序博客网 时间:2024/06/05 17:16
Substrings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8504 Accepted Submission(s): 3931
Problem Description
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.
Input
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.
Output
There should be one line per test case containing the length of the largest string found.
Sample Input
23ABCDBCDFFBRCD2roseorchid
Sample Output
22
Author
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[N][N];int len[N];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[N][N];int len[N];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;}*/
0 0
- HDU 1238 Substrings(求公共正反向连续子串)
- poj1226 Substrings (多串最长公共子串+正反)
- HDU 4681 String 正反求最长公共子序列+枚举
- HDU--1238-Substrings(-最长连续子串)
- HDU 1238 Substrings (最长公共子串+DFS)
- 求最长连续公共子串
- hdu 1238 Substrings (最长相同连续子序列)
- poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题
- 正反求取最大公共子串
- 求两个字符串的最长的连续公共子串
- 求两个字符串的最长的连续公共子串
- 求两个字符串的最长的连续公共子串
- 求两个字符串的最长的连续公共子串
- 求两个字符串的最长连续公共子串
- 求两字符串的最长公共连续子串
- 求两字符串最长公共连续子串---C++编程
- HDU1238 Substrings(kmp,最长公共子串)
- POJ 3415 Common Substrings (求长度大于K的公共子串个数,5级)
- Scala教程(十)函数与闭包详解
- HDU - 3496 Watch The Movie(01背包)
- HTML5中的表单+Ajax+localStorage+Cookie
- java实现链表的反转
- Java中Collection和Collections的区别
- HDU 1238 Substrings(求公共正反向连续子串)
- 算法代码:查找数组中第i小的元素
- 关于Java中多个数据excel表格的导出
- Linux线程通信条件变量理解
- VS2010/MFC编程之树型控件的创建及CTreeCtrl类的成员函数和实例
- Android数据存储之文件 openFileOutput & openFileInput
- 测试用例知识,总结边界值,因果图,等价类划分法
- win7跨网段共享解决方法
- HDU - 2844 Coins(多重背包)