We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a5e06c0 commit 9d8e130Copy full SHA for 9d8e130
geekforgeeks/DP/a.out
43.2 KB
geekforgeeks/DP/longestRepeatedSubsequence.cpp
@@ -0,0 +1,43 @@
1
+#include<bits/stdc++.h>
2
+using namespace std;
3
+
4
+string longestRepeat(string a)
5
+{
6
+ int n=a.size();
7
+ vector< vector<int> >dp(n+1);
8
+ for(int i=0;i<n+1;i++){
9
+ dp[i].resize(n+1);
10
+ }
11
+ for(int i=1;i<n+1;i++){
12
+ for(int j=1;j<n+1;j++){
13
+ if(a[i-1]==a[j-1] && i!=j)
14
+ dp[i][j]=1+dp[i-1][j-1];
15
+ else
16
+ dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
17
18
19
+ string rev="";
20
+ int i=n,j=n;
21
+ while(i>0 && j>0){
22
+ if(dp[i][j]==1+dp[i-1][j-1])
23
+ {
24
+ rev+=a[i-1];
25
+ i--;
26
+ j--;
27
28
+ else if(dp[i][j]==dp[i-1][j-1])
29
30
31
32
33
+ reverse(rev.begin(),rev.end());
34
+ return rev;
35
+}
36
37
+int main()
38
39
+ string a;
40
+ cin>>a;
41
+ cout<<longestRepeat(a)<<endl;
42
+ return 0;
43
0 commit comments