题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
#include <iostream> #include <vector> using namespace std; int main() { string str; while (cin >> str) { int n = str.size(); int maxD = 1; vector<vector<bool>> dp(n,vector<bool>(n, false)); for (int i = n-1; i >= 0; --i) { for (int j = i+1; j < n; ++j) { if (j-i<=2) dp[i][j] = str[i]==str[j]; else dp[i][j] = str[i]==str[j]&&dp[i+1][j-1]; if (dp[i][j]) maxD = max(maxD,j-i+1); } } cout<<maxD<<endl; } }