这道题我也是花了好长时间才写出来,本来想出来方法了,带式写着太麻烦,所以就一直没写,一直想用string类写的,但是由于对string类中的函数不清楚,所以基本上把每个函数都试了一遍,,,,好在写出来了。这道题题意容易搞错,去年省赛时就有许多队伍理解错题意了,此题不是让求最长回文子串的,而是让求一个最长的子串,该子串的逆串也在原串中。举个例子来说,abcdba,若是求最长回文子串,应为a,对此题来说,应为ab,因为ab的逆串ba也在原串中。题目:
Substring
-
描述
-
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.
-
输入
- 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'). 输出
- Output for each test case the longest substring of input such that the reversal of the substring is also a substring of input 样例输入
-
3 ABCABA XYZ XCVCX
样例输出
-
ABA X XCVCX
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <string.h>
using namespace std;
int main(){
//freopen("1.txt","r",stdin);
int numcase;
scanf("%d",&numcase);
string s1,s2,s3;
while(numcase--){
cin>>s1;
s2=s1;
int max=0;
reverse(s2.begin(),s2.end());
int len=s1.size();
for(int i=0;i<len;++i){
for(int j=1;j<=len-i;++j){
string::size_type pos=s2.find(s1.substr(i,j));
if(pos!=string::npos){
if(max<j){
max=j;
s3=s1.substr(i,j);
}
}
}
}
cout<<s3<<endl;
}
return 0;
}