feat: new dp problem formatted
This commit is contained in:
33
longest-palindromic-subsequence/src/ac.cpp
Normal file
33
longest-palindromic-subsequence/src/ac.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user