forked from aman2382000/Dynamic-Programming
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLCS.java
More file actions
38 lines (38 loc) · 896 Bytes
/
LCS.java
File metadata and controls
38 lines (38 loc) · 896 Bytes
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
import java.util.*;
public class LCS {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
int l1 = a.length();
int l2 = b.length();
int dp[][] = new int[l1 + 1][l2 + 1];
int i, j;
for (i = 0; i <= l1; i++)
dp[i][0] = 0;
for (i = 0; i <= l2; i++)
dp[0][i] = 0;
StringBuffer s = new StringBuffer();
for (i = 0; i < l1; i++) {
for (j = 0; j < l2; j++) {
if (a.charAt(i) == b.charAt(j))
dp[i + 1][j + 1] = 1 + dp[i][j];
else
dp[i + 1][j + 1] = Math.max(dp[i + 1][j], dp[i][j + 1]);
}
}
i = l1;
j = l2;
while (i > 0 && j > 0) {
if (dp[i][j] != dp[i][j - 1]) {
s = s.append(Character.toString(b.charAt(j - 1)));
i = i - 1;
j = j - 1;
} else {
j--;
}
}
// System.out.println(dp[l1][l2]); length of LCS
System.out.print(s.reverse());
}
}