1 /*Substring 2 时间限制:1000 ms | 内存限制:65535 KB 3 难度:1 4 描述 5 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, 6 return the string that occurs earliest in input. 7 8 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 9 one character substring, so we implement the tie-breaker rule of taking the earliest one first. 10 11 输入 12 The first line of input gives a single integer, 1 ≤ N ≤ 10, the number of test cases. Then follow, for each test case, a line containing between 1 and 50 characters, inclusive. Each character of input will be an uppercase letter ('A'-'Z'). 13 输出 14 Output for each test case the longest substring of input such that the reversal of the substring is also a substring of input 15 样例输入 16 3 17 ABCABA 18 XYZ 19 XCVCX 20 样例输出 21 ABA 22 X 23 XCVCX 24 来源 25 第四届河南省程序设计大赛 26 上传者 27 张云聪 28 29 */ 30 #include<stdio.h> 31 #include<string.h> 32 int main() 33 { 34 int n, i, j, k, m, real, l; 35 char a[52], b[52]; 36 scanf("%d", &n); 37 while(n--) 38 { 39 real = 0; 40 scanf("%s", a); 41 m = strlen(a); 42 for(i = m; i>=1; i--){ // 字符串长度 43 for(j = 0; j <= m-i; j++)// 字符串起始位置 44 { 45 for(k = 0; k < i; k++) 46 b[k] = a[i+j-k-1]; 47 b[i] = '\0'; 48 if( strstr( a , b ) ) //求出substring 49 { 50 for(l = i-1; l>=0; l--) 51 printf("%c", b[l]); // 输出 52 printf("\n"); 53 real = 1; 54 break; 55 } 56 } 57 if(real) break; 58 } 59 } 60 return 0; 61 }