forked from vivekanand44/codes-Youtube-videos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest Palindromic Subsequence.cpp
84 lines (75 loc) · 1.34 KB
/
Longest Palindromic Subsequence.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "abcecqba";
int n = str.length();
int a[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
a[i][j] = 1;
else
a[i][j] = 0;
}
}
int l = 1;
while(l <= n-1)
{
int i = 0;
while(i+l <=n-1)
{
int j = i+l;
if(str[i] == str[j])
a[i][j] = a[i+1][j-1] + 2;
else
{
if(a[i+1][j] > a[i][j-1])
a[i][j] = a[i+1][j];
else
a[i][j] = a[i][j-1];
}
i = i + 1;
}
l = l + 1;
}
// cout << a[0][n-1];
int palindrome_length = a[0][n-1];
char palindrome[palindrome_length];
palindrome[palindrome_length] = '\0';
int i = 0;
int j = n-1;
int palindrome_start = 0;
int palindrome_end = palindrome_length-1;
while(palindrome_end - palindrome_start >=1)
{
if(str[i] == str[j])
{
palindrome[palindrome_start] = str[i];
palindrome[palindrome_end] = str[i];
palindrome_start = palindrome_start + 1;
palindrome_end = palindrome_end - 1;
i = i+1;
j = j-1;
}
else
{
if(a[i+1][j] > a[i][j-1])
{
i = i+1;
}
else
{
j = j-1;
}
}
}
if(palindrome_end == palindrome_start)
palindrome[palindrome_end] = str[i];
cout <<"\n Lenght of the LPS is =>" << palindrome_length;
cout <<"\n The LPS is =>" << palindrome;
return 0;
}