Skip to content

Commit 58bb43b

Browse files
committed
implemented a solution to find longest repeating subsequence
1 parent c1e80c5 commit 58bb43b

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,63 @@
11
#include<iostream>
2+
#include<string>
23

34
using namespace std;
45

56
//longest repeating subsequence
7+
string LRS(string a,int m)
8+
{
9+
int table[m+1][m+1];
10+
11+
for (int i=0; i<=m; i++)
12+
for (int j=0; j<=m; j++)
13+
table[i][j] = 0;
14+
15+
for(int i = 1 ; i <= m;i++)
16+
{
17+
for(int j = 1 ; j<= m;j++)
18+
{
19+
if(i==0||j==0)
20+
table[i][j] = 0;
21+
22+
else if(a[i-1]==a[j-1] && i!=j)
23+
table[i][j] = table[i-1][j-1] + 1;
24+
25+
else table[i][j] = max(table[i-1][j],table[i][j-1]);
26+
}
27+
}
28+
29+
30+
31+
int i = m,j=m;
32+
string lrs= " ";
33+
while(i>0 && j>0)
34+
{
35+
if(table[i][j]==table[i-1][j-1]+1)
36+
{
37+
lrs = lrs + a[i-1];
38+
39+
i--;
40+
j--;
41+
42+
}
43+
44+
else if(table[i][j] == table[i-1][j])
45+
i--;
46+
47+
else
48+
j--;
49+
}
50+
51+
return lrs;
52+
}
53+
654

755
int main()
856
{
957

58+
59+
string s1 = "AABEBCDD";
60+
int len = s1.length();
61+
62+
cout<<LRS(s1,len);
1063
}

Dynamic Programming/PrintingTheLCS.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ void PrintLCS(string &a,string &b,int m,int n)
3333
//getting the length of LCS
3434
//making the array to store the common letters
3535
string LCS(LCSlen+1,'\0');
36-
NULL
3736

3837
int i = m , j = n;
3938

0 commit comments

Comments
 (0)