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
1 #include <stdio.h> 2 #include <string.h> 3 int map[60][60]; 4 int main() 5 { 6 int T; 7 scanf("%d",&T); 8 while(T--) 9 { 10 int i,j,len,max=0,t; 11 char str1[60],str2[60]; 12 memset(map,0,sizeof(map)); 13 scanf("%s",str1); 14 len = strlen(str1); 15 for(i=0;i<len;i++) 16 str2[i]=str1[len-1-i]; 17 for(i=0;i<len;i++) 18 { 19 for(j=0;j<len;j++) 20 { 21 if(str1[i]==str2[j]) 22 map[i+1][j+1] = map[i][j]+1; 23 if(map[i+1][j+1]>max) 24 { 25 max = map[i+1][j+1]; 26 t = i+1; 27 } 28 } 29 } 30 for(i=t-max;i<t;i++) 31 printf("%c",str1[i]); 32 printf("\n"); 33 } 34 return 0; 35 }
//最长公共子串
nyoj_308_Substring_201405091611,布布扣,bubuko.com
nyoj_308_Substring_201405091611
原文:http://www.cnblogs.com/xl1027515989/p/3719326.html