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
Solution:
// Talk is cheap, show me the code
// Created by Misdirection 2021-08-16 15:43:11
// All rights reserved.
#include <iostream>
#include <vector>
using namespace std;
int main(){
string s;
getline(cin, s);
int len = s.length();
vector<vector<bool>> dp(len, vector<bool>(len, false));
int ans = 1;
for(int L = 0; L < len; ++L){
for(int i = 0; i < len - L; ++i){
if(L == 0){
dp[i][i] = true;
continue;
}
int j = i + L;
if(s[i] == s[j]){
if(L <= 2) dp[i][j] = true;
else dp[i][j] = dp[i + 1][j - 1];
if(dp[i][j] == true && ans < L + 1) ans = L + 1;
}
else dp[i][j] = false;
}
}
printf("%d\n", ans);
return 0;
}