-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
41 lines (35 loc) · 1.17 KB
/
Main.java
File metadata and controls
41 lines (35 loc) · 1.17 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
String s2 = br.readLine();
int[][] arr = new int[s1.length()+1][s2.length()+1];
for(int i=0; i<arr.length; i++){
Arrays.fill(arr[i], 0);
}
for(int i=1; i<arr.length; i++){
for(int j=1; j<arr[i].length; j++){
if(s1.charAt(i-1) != s2.charAt(j-1)){
arr[i][j] = Math.max(arr[i-1][j], arr[i][j-1]);
} else {
// s1.charAt(i-1) == s2.charAt(j-1)
arr[i][j] = arr[i-1][j-1] + 1;
}
}
}
/*
for(int i=1; i<arr.length; i++){
for(int j=1; j<arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
*/
System.out.println(arr[s1.length()][s2.length()]);
br.close();
}
}