-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLCS.java
111 lines (98 loc) · 2.07 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.*;
class LCS
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of first sequence");
int n1=sc.nextInt();
char[] input1=new char[n1];
System.out.println("Enter Characters of first sequence");
for(int i=0;i<n1;i++)
{
input1[i]=sc.next().charAt(0);
}
System.out.println("Enter size of second sequence");
int n2=sc.nextInt();
char[] input2=new char[n2];
System.out.println("Enter Characters of second sequence");
for(int i=0;i<n2;i++)
{
input2[i]=sc.next().charAt(0);
}
char[][] b=new char[n1+1][n2+1];
int[][] c=new int[n1+1][n2+1];
for(int i=0;i<=n1;i++)
{
c[i][0]=0;
b[i][0]='0';
}
for(int j=0;j<=n2;j++)
{
c[0][j]=0;
b[0][j]='0';
}
for(int i=1;i<=n1;i++)
{
for(int j=1;j<=n2;j++)
{
if(input1[i-1]==input2[j-1])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]='\\';
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]='|';
}
else if(c[i][j-1]>=c[i-1][j])
{
c[i][j]=c[i][j-1];
b[i][j]='-';
}
}
}
System.out.println();
System.out.println("Matrix for calculating size of LCS ==> ");
for(int i=0;i<=n1;i++)
{
for(int j=0;j<=n2;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
System.out.println();
System.out.println("Path Matrix for calculating LCS ==> ");
for(int i=0;i<=n1;i++)
{
for(int j=0;j<=n2;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
System.out.println();
System.out.print("Longest Common SubSequence is---> ");
new LCS().printLcs(b,input1,n1,n2);
}
public void printLcs(char[][] b,char[] input1,int i,int j)
{
if(i==0 || j==0)
{
return;
}
if(b[i][j]=='\\')
{
printLcs(b,input1,i-1,j-1);
System.out.print(input1[i-1]+" ");
}
else if(b[i][j]=='|')
{
printLcs(b,input1,i-1,j);
}
else
printLcs(b,input1,i,j-1);
}
}