-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCS.java
71 lines (71 loc) · 1.65 KB
/
LCS.java
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
import java.util.*;
class LCS {
static void lcs(String X, String Y, int m, int n)
{
int[][] L = new int[m + 1][n + 1];
char[][] A = new char[m+1][n+1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X.charAt(i - 1) == Y.charAt(j - 1)){
L[i][j] = L[i - 1][j - 1] + 1;
A[i][j] = 'd';
}
else{
if(L[i-1][j] > L[i][j-1]){
L[i][j] = L[i-1][j];
A[i][j] = 'u';
}
else{
L[i][j] = L[i][j-1];
A[i][j] = 'l';
}
}
}
}
int index = L[m][n];
int temp = index-1;
char[] lcs = new char[index];
int i = m; int j = n;
while (i > 0 && j > 0) {
if(A[i][j]=='d'){
lcs[temp] = X.charAt(i-1);
i--; j--;temp--;
}
else if(A[i][j] == 'u'){
i--;
}
else if(A[i][j] == 'l'){
j--;
}
}
System.out.println("Matrix L:");
for(i=0; i<m+1; i++){
for(j=0; j<n+1; j++){
System.out.print(L[i][j] + " ");
}
System.out.println();
}
System.out.println("Matrix A:");
for(i=1; i<m+1; i++){
for(j=1; j<n+1; j++){
System.out.print(A[i][j] + " ");
}
System.out.println();
}
System.out.print("LCS of " + X + " and " + Y + " is ");
for (int k = 0; k < index; k++)
System.out.print(lcs[k]);
System.out.println("\nLength = " + index);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter two words: ");
String X = sc.next();
String Y = sc.next();
int m = X.length(); int n = Y.length();
lcs(X, Y, m, n);
}
}