标签:
You are given a string input. You are to find the longest substring of input such that the reversal of the substring is also a substring of input. In case of a tie, return the string that occurs earliest in input.
Note well: The substring and its reversal may overlap partially or completely. The entire original string is itself a valid substring . The best we can do is find a one character substring, so we implement the tie-breaker rule of taking the earliest one first.
3 ABCABA XYZ XCVCX
ABA X XCVCX
思路:用二维的矩阵来做。。
#include<iostream> #include<string.h> using namespace std; int main() { int num[51][51]; char a[51],b[51]; int T,i,j,k,len,max,x,y,start; cin>>T; while(T--) { memset(num,0,sizeof(num)); k=0; max=0; cin>>a; //ABCABA len=strlen(a); for(i=len-1;i>=0;i--) { b[k++]=a[i]; } b[k]='\0'; for(i=1;i<=len;i++) { for(j=1;j<=len;j++) { if(a[i-1]==b[j-1]) { num[i][j]=num[i-1][j-1]+1; if(num[i][j]>max) { max=num[i][j];//max最大的长度 x=i; y=j; } } } } start=x-max; for(i=start;i<start+max;i++) cout<<a[i]; cout<<endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/zuguodexiaoguoabc/article/details/44408635