Files
problemas-para-competicao-p…/longest-palindromic-subsequence/src/ac.cpp
2025-12-19 07:49:39 -03:00

33 lines
590 B
C++

#include <bits/stdc++.h>
using namespace std;
int main()
{
int n; cin >> n;
string s; cin >> s;
/*
dp[i][j] says if the substring from i to j is a palindrome
*/
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int i = n - 1; i >= 0; i--)
{
dp[i][i] = 1;
for (int j = i + 1; j < n; j++)
{
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]);
}
}
}
cout << dp[0][n - 1] << endl;
return 0;
}