许诺 • 13天前
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int longestPalindromeSubseq(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
dp[i][i] = 1;
}
for (int l = 2; l <= n; l++) {
for (int i = 0; i <= n - l; i++) {
int j = i + l - 1;
if (s[i] == s[j]) {
dp[i][j] = dp[i+1][j-1] + 2;
} else {
dp[i][j] = max(dp[i+1][j], dp[i][j-1]);
}
}
}
return dp[0][n-1];
}
int main() {
string s;
cin >> s;
cout << longestPalindromeSubseq(s) << endl;
return 0;
}
评论:
请先登录,才能进行评论