-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10405 Longest Common Subsequence.cpp
59 lines (54 loc) · 1.13 KB
/
10405 Longest Common 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
#include <bits/stdc++.h>
using namespace std;
int pr[1005][1005], dp[1005][1005];
string x, y;
int lcs (int m, int n)
{
for(int i = 1; i <= m; i++)
dp[i][0] = 0;
for(int j = 0; j <= n; j++)
dp[0][j] = 0;
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
if(x[i-1] == y[j - 1])
{
dp[i][j] = dp[i - 1][j - 1] + 1;
pr[i][j] = 1;
}
else if(dp[i - 1][j] >= dp[i][j - 1])
{
dp[i][j] = dp[i - 1][j];
pr[i][j] = 2;
}
else
{
dp[i][j] = dp[i][j - 1];
pr[i][j] = 3;
}
}
}
return dp[m][n];
}
int main()
{
while(getline(cin,x ))
{
getline(cin, y);
memset(dp,0,sizeof dp);
int m = x.size(), n = y.size();
int ans = lcs(m,n);
cout<< ans << endl;
/*
for(int i = 0; i <= m; i++)
{
for(int j = 0; j <= n; j++)
{
cout<< dp[i][j] << " ";
}
cout<< endl;
}
*/
}
}