https://www.patest.cn/contests/pat-a-practise/1040
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <unordered_map>
#include <string>
#include <queue>
#include <unordered_set>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1010;
char s[MAXN];
bool dp[MAXN][MAXN];
int ans = 1;
void init(const int length) {
ans = 1;
memset(dp, 0, sizeof(dp)); //init 0
// boundary condition
for (int i = 0; i < length; i++) {
dp[i][i] = true;
if (i < length-1 && s[i] == s[i+1]) {
dp[i][i+1] = true;
ans = 2;
}
}
// cout << ans << endl;
}
void state_move(const int length) {
//步长迭代
for (int L = 3; L <= length; L++) {
for (int i = 0; i + L - 1 < length; i++) {
int j = i + L - 1; //right point
if (s[i] == s[j] && dp[i+1][j-1]) {
dp[i][j] = true;
ans = L; //update Longest ans
}
}
}
}
int main(int argc, char const *argv[])
{
gets(s);
int length = strlen(s);
init(length);
state_move(length);
cout << ans << endl;
return 0;
}
/**
Sample Input:
Is PAT&TAP symmetric?
Sample Output:
11
<https://www.patest.cn/contests/pat-a-practise/1040>
<http://hihocoder.com/problemset/problem/1032>
**/