-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlongestSubSequence.cpp
63 lines (54 loc) · 1.14 KB
/
longestSubSequence.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
#include <iostream>
#include <string.h>
using namespace std;
int lcs[100][100];
int maximum;
int max(int a, int b)
{
int u;
if (a > b
? u = a
: u = b)
;
return u;
}
int subSeq(char str1[], char str2[], int len1, int len2, int i, int j)
{
maximum = lcs[1][1];
for (i = 1; i <= len1; i++)
{
for (j = 1; j <= len2; j++)
{
if (str1[i - 1] == str2[j - 1])
{
cout << str1[i - 1];
lcs[i][j] = 1 + lcs[i - 1][j - 1];
}
else
lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
if (maximum > lcs[i][j] ?: maximum = lcs[i][j])
;
}
}
cout << maximum << endl;
}
int main()
{
char one, two;
char str1[100], str2[100];
int n, i, j;
cin >> str1 >> str2;
int max;
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1 > len2 ? max = len1 : max = len2)
;
for (i = 0; i < len1; i++)
{
for (j = 0; j < len2; j++)
{
lcs[i][j] = 0;
}
}
subSeq(str1, str2, len1, len2, i, j);
}