-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLcsDP.java
More file actions
63 lines (55 loc) · 1.2 KB
/
LcsDP.java
File metadata and controls
63 lines (55 loc) · 1.2 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
import java.util.Scanner;
public class LcsDP {
public static void main(String[] args) {
// 비교할 두 문자열을 입력받는다(x, y).
Scanner sc = new Scanner(System.in);
String x = sc.next();
String y = sc.next();
sc.close();
// c에는 x,y의 최장 공통부분의 길이정보를, b에는 경로정보를 저장한다.
int m = x.length();
int n = y.length();
int c[][] = new int[m+1][n+1];
int b[][] = new int[m+1][n+1];
for(int i=0;i<=m;i++) {
c[i][0] = 0;
}
for(int j=1;j<=n;j++) {
c[0][j]=0;
}
for(int i=1; i<=m; i++) {
for(int j=1;j<=n;j++) {
if(x.charAt(i-1) == y.charAt(j-1)) {
c[i][j] = c[i-1][j-1]+1;
b[i][j] = 'D';
}
else {
if(c[i-1][j] >= c[i][j-1]) {
c[i][j] = c[i-1][j];
b[i][j] = 'U';
}
else {
c[i][j] = c[i][j-1];
b[i][j] = 'L';
}
}
}
}
// b를 이용하여 공통부분 경로를 구한다.
int a = n;
String sq ="";
for(int i=m; 1<=i; i--) {
if(b[i][a] == 'D') {
char tmp = x.charAt(i-1);
sq = tmp + sq;
a--;
}
else if(b[i][a] == 'L') {
a--;
i++;
}
}
System.out.println(c[m][n]);
System.out.println(sq);
}
}