-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprint_lcs.java
74 lines (62 loc) · 1.69 KB
/
print_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
import java.util.Scanner;
class print_lcs {
public String longestCommonSubsequence(String text1, String text2) {
int n = text1.length();
int m = text2.length();
return LCS(text1,text2,n,m);
}
private String LCS (String text1,String text2,int n,int m)
{
int[][] t = new int[1001][1001];
for(int i=0;i<m+1;i++)
{
for(int j=0;j<n+1;j++)
{
if (i==0 || j==0);
{
t[i][j] = 0;
}
}
}
for (int i = 1;i<n+1;i++)
{
for (int j = 1;j<m+1;j++)
{
if (text1.charAt(i-1) == text2.charAt(j-1))
{
t[i][j] = 1+t[i-1][j-1];
}else{
t[i][j] = Math.max(t[i-1][j],t[i][j-1]);
}
}
}
int i=n;
int j=m;
StringBuilder str2 = new StringBuilder();
while (i>0 && j>0)
{
if(text1.charAt(i-1) == text2.charAt(j-1))
{
str2.append(text1.charAt(i-1));
i--;
j--;
}else
{
if (t[i][j-1] > t[i-1][j])
{
j--;
}else{
i--;
}
}
}
return str2.reverse().toString();
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String one = s.next();
String two = s.next();
print_lcs print_lcs = new print_lcs();
System.out.println(print_lcs.longestCommonSubsequence(one , two));
}
}