Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 89ce46a

Browse files
authored
Merge pull request fnplus#564 from nikhilgupta30/nikhil
Basic Dynamic Programming Problems
2 parents 36348e4 + 6c2c6bd commit 89ce46a

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Dynamic Programming C++ implementation of Longest Common Subseq. problem */
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
int lcs(string X, string Y, int m, int n ) {
6+
int L[m + 1][n + 1];
7+
int i, j;
8+
9+
for (i = 0; i <= m; i++) {
10+
for (j = 0; j <= n; j++) {
11+
if (i == 0 || j == 0)
12+
L[i][j] = 0;
13+
14+
else if (X[i - 1] == Y[j - 1])
15+
L[i][j] = L[i - 1][j - 1] + 1;
16+
17+
else
18+
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
19+
}
20+
}
21+
22+
return L[m][n];
23+
}
24+
25+
// main function
26+
int main(){
27+
28+
string X,Y;
29+
cout<<"enter first string: ";
30+
cin>>X;
31+
cout<<"\nenter second string: ";
32+
cin>>Y;
33+
34+
int m = X.size();
35+
int n = Y.size();
36+
37+
cout << "Length of LCS is "
38+
<< lcs( X, Y, m, n );
39+
40+
return 0;
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Dynamic Programming C++ implementation of Longest commong substring problem */
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
int lcs(string X, string Y, int m, int n ) {
6+
int LCSuff[m + 1][n + 1];
7+
int result = 0;
8+
9+
for (int i = 0; i <= m; i++) {
10+
for (int j = 0; j <= n; j++) {
11+
if (i == 0 || j == 0)
12+
LCSuff[i][j] = 0;
13+
else if (X[i-1] == Y[j-1]) {
14+
LCSuff[i][j] = LCSuff[i-1][j-1] + 1;
15+
result = max(result, LCSuff[i][j]);
16+
}
17+
else LCSuff[i][j] = 0;
18+
}
19+
}
20+
21+
return result;
22+
}
23+
24+
// main function
25+
int main(){
26+
27+
string X,Y;
28+
cout<<"enter first string: ";
29+
cin>>X;
30+
cout<<"\nenter second string: ";
31+
cin>>Y;
32+
33+
int m = X.size();
34+
int n = Y.size();
35+
36+
cout << "Length of LCS is "
37+
<< lcs( X, Y, m, n );
38+
39+
return 0;
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Dynamic Programming C++ implementation of LIS problem */
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
int lis( int arr[], int n ) {
6+
int lis[n];
7+
8+
lis[0] = 1;
9+
10+
for (int i = 1; i < n; i++ ) {
11+
lis[i] = 1;
12+
for (int j = 0; j < i; j++ )
13+
if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
14+
lis[i] = lis[j] + 1;
15+
}
16+
17+
return *max_element(lis, lis+n);
18+
}
19+
20+
// main function
21+
int main() {
22+
int n;
23+
cout<<"enter size of array: ";
24+
cin>>n;
25+
26+
int arr[n];
27+
cout<<"enter elements: \n";
28+
for(int i=0;i<n;i++) cin>>arr[i];
29+
30+
printf("Length of lis is %d\n", lis( arr, n ) );
31+
32+
return 0;
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /