hdu1238 暴力搜
Substrings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9122 Accepted Submission(s): 4302
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
2
3
ABCD
BCDFF
BRCD
2
rose
orchid
Sample Output
2
2
思路:要求最长公共连续字串的,必然要从原来的最短的串那个入手,先找出那个序列,暴力分析所有的情况,一一匹配最大的情况,匹配连续序列时,可以用stl
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <stack> #include <queue> #include <string> #include <algorithm> const int inf = (1<<31)-1; const int MAXN = 1e2+10; using namespace std; string s[MAXN]; int main() { int t,n,ti,mmin; string ts1,ts2; scanf("%d",&t); while(t--){ scanf("%d",&n); mmin = inf; for(int i=0;i<n;i++){ //scanf("%s",s[i]); cin>>s[i]; if(s[i].length()<mmin){ mmin = s[i].length(); ti = i; } } int mmax = 0,k; /* for(int i=mmin;i>0;i--){ for(int j=0;j<mmin-i+1;j++){ ts1 = s[ti].substr(j,i); //startpos lenth ts2 = ts1; reverse(ts2.begin(),ts2.end()); // cout<<ts1<<" "<<ts2<<endl; for(k=0;k<n;k++){ if(s[k].find(ts1,0)==-1&&s[k].find(ts2,0)==-1) break; } if(k==n&&mmax<ts1.length()) mmax = ts1.length(); } }*/ char ms1[MAXN],ms2[MAXN]; for(int i=0;i<mmin;i++){ for(int j=i;j<mmin;j++){ int b = 0; for(int w=i;w<=j;w++){ ms1[b] = s[ti][w]; ms2[b] = s[ti][j-b]; b++; } //cout<<ms1<<" "<<ms2<<endl; ms1[b] = ms2[b] = ‘\0‘; //printf("%s %s\n",ms1,ms2); for(k=0;k<n;k++){ if(s[k].find(ms1,0)==-1&&s[k].find(ms2,0)==-1){ break; } } if(k==n&&mmax<b) mmax = b; } } cout<<mmax<<endl; } //cout << "Hello world!" << endl; return 0; } /* 3 123 23333 12222 */