题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344
套最长回文字串的模版,不过让我发现了必须先处理长度为1、2两种情况的,不然会有两个测试点不过
代码
#include<algorithm>#include<iostream>#include<string>using namespace std;const int maxn = 1010;int dp[maxn][maxn] = {0};string input;int main(){int ans = 1;getline(cin, input);for(int i = 0; i < input.size(); i++){dp[i][i] = 1;if(i < input.size() - 1){if(input[i]==input[i+1]){dp[i][i+1] = 1;ans = 2;}}}for(int L = 3; L <= input.size(); L++){for(int i = 0; i + L -1 < input.size(); i++){int j = i + L - 1;if(input[i] == input[j] && dp[i + 1][j - 1] == 1){dp[i][j] = 1;ans = L;}}}printf("%d", ans);return 0;}
