-
Notifications
You must be signed in to change notification settings - Fork 77
/
lps.cpp
27 lines (25 loc) · 903 Bytes
/
lps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Returns the length of the longest palindromic subsequence in seq
int lps(string const& s) {
int n = (int)s.length();
// Strings of length 1 are palindrome of length 1
vector<vector<int>> dp(n, vector<int>(n, 0));
for(int i = 0; i < n; i++) dp[i][i] = 1;
/* Build the table. Note that the lower diagonal values of table are
useless and not filled in the process. The values are filled in a
manner similar to MCM. */
for(int len = 2; len <= n; len++) {
for (int i = 0; i < n - len + 1; i++) {
int j = i + len - 1;
if (s[i] == s[j] and len == 2) {
dp[i][j] = 2;
}
else if (s[i] == s[j]) {
dp[i][j] = dp[i + 1][j - 1] + 2;
}
else {
dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]);
}
}
}
return dp[0][n - 1];
}