-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy path10405.cpp
More file actions
executable file
·77 lines (65 loc) · 1.53 KB
/
10405.cpp
File metadata and controls
executable file
·77 lines (65 loc) · 1.53 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Problem: Longest Common Subsequence UVa 10405
Programmer: Md. Mahmud Ahsan
Description: LCS
Compiled: Visual C++ 7.0
Date: 20-11-05
*/
#include <iostream>
#include <string>
using namespace std;
#define N 1005
//Global Declaration
int c[N+1][N+1];
//char b[N+1][N+1];
int LCSLength(char *x, char *y, int m, int n);
//void printLCS(char *x, int i, int j); // i for x and j for y
int main(){
char x[N], y[N];
while (cin.getline(&x[1], sizeof(x))){
cin.getline(&y[1], sizeof(y));
int m = strlen(&x[1]); // as string index begins from 1
int n = strlen(&y[1]); // as string index begins from 1
int len = LCSLength(x, y, m, n);
cout << len << endl;
//printLCS(x, m, n);
//cout << endl;
}
return 0;
}
int LCSLength(char *x, char *y, int m , int n){
int i, j;
/* initialize all to zero */
for (i = 0; i <= m; ++i)
for (j = 0; j <= n; ++j)
c[i][j] = 0;
for (i = 1; i <= m; ++i){
for (j = 1; j <= n; ++j){
if (x[i] == y[j]){
c[i][j] = c[i-1][j-1] + 1;
//b[i][j] = '#'; //# for angle arrow
}
else if (c[i-1][j] >= c[i][j-1]){
c[i][j] = c[i-1][j];
//b[i][j] = '^'; //^ for up arrow
}
else{
c[i][j] = c[i][j-1];
//b[i][j] = '<'; //< for left arrow
}
}
}
return c[m][n];
}
/*
void printLCS(char *x, int i, int j){
if (i == 0 || j == 0) return;
if (b[i][j] == '#'){
printLCS(x, i-1, j-1);
cout << x[i]<< " ";
}
else if (b[i][j] == '^')
printLCS(x, i-1, j);
else
printLCS(x, i, j-1);
}
*/