-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXauConChungDaiNhat.java
More file actions
60 lines (56 loc) · 1.34 KB
/
XauConChungDaiNhat.java
File metadata and controls
60 lines (56 loc) · 1.34 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
package dynamic;
import java.util.Scanner;
/**
* Created by linh on 17/05/2017.
*
Input
abcqweqrqtwt
xvnbmnabcsdq
Output Table:
4 4 4 4 4 4 4 3 2 1 1 1
3 3 3 3 3 3 3 3 2 1 1 1
2 2 2 2 2 2 2 2 2 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
*/
public class Xauconchungdainhat
{
String st1,st2;
int L[][];
void input(){
Scanner scanner = new Scanner(System.in);
st1 = scanner.next();
st2 = scanner.next();
}
int qhd(){
int s1 = st1.length();
int s2 = st2.length();
L = new int[s1+1][s2+2];
for (int i = 0;i<s1;i++) {
L[i][0] = 0;
}
for (int i = 0;i<s2;i++) {
L[0][i] = 0;
}
L[s1][s2] = 0;
for (int i = s1-1;i>=0;i--)
for (int j = s2-1;j>=0;j--)
if (st1.charAt(i) == st2.charAt(j)){
L[i][j] = 1 + L[i+1][j+1];
}
else L[i][j] = Math.max(L[i+1][j],L[i][j+1]);
return L[0][0];
}
public static void main(String[] args) {
Xauconchungdainhat x = new Xauconchungdainhat();
x.input();
System.out.println(x.qhd());
}
}