Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?
, the longest symmetric sub-string is s PAT&TAP s
, hence you must output 11
.
Input Specification:
Each input file contains one test case which gives a non-empty string of length no more than 1000.
Output Specification:
For each test case, simply print the maximum length in a line.
Sample Input:
Is PAT&TAP symmetric?
Sample Output:
11
解题思路:
最长回文字串问题:
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;
const int MANX = 1010;
string input;
int sdp[MANX][MANX];
int main() {
getline(cin, input);
int len = input.length();
memset(sdp, 0, sizeof(sdp));
int ans = 1;
for (int i = 0; i < len; ++i) {
sdp[i][i] = 1;
if (i < len - 1) {
if (input[i] == input[i + 1]) {
sdp[i][i + 1] = 1;
ans = 2;
}
}
}
//状态转移
for (int i = 3; i <= len; ++i) {
for (int j = 0; j + i - 1 < len; ++j) {
int k = j + i - 1;
if (input[j] == input[k] && sdp[j + 1][k - 1] == 1) {
sdp[j][k] = 1;
ans = i;
}
}
}
cout << ans << endl;
system("PAUSE");
return 0;
}