File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ package backjoon ;
2
+ // https://www.acmicpc.net/problem/9251
3
+
4
+ import java .io .BufferedReader ;
5
+ import java .io .IOException ;
6
+ import java .io .InputStreamReader ;
7
+
8
+ public class _9251 {
9
+ public static void main (String [] args ) throws IOException {
10
+
11
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
12
+ char [] str1 = br .readLine ().toCharArray ();
13
+ char [] str2 = br .readLine ().toCharArray ();
14
+
15
+ int length_1 = str1 .length ;
16
+ int length_2 = str2 .length ;
17
+
18
+ int [][] dp = new int [length_1 + 1 ][length_2 + 1 ];
19
+
20
+ for (int i = 1 ; i <= length_1 ; i ++) {
21
+ for (int j = 1 ; j <= length_2 ; j ++) {
22
+
23
+ // (i-1)과 (j-1) 번째 문자가 서로 같은 경우
24
+ if (str1 [i - 1 ] == str2 [j - 1 ]) {
25
+ dp [i ][j ] = dp [i - 1 ][j - 1 ] + 1 ;
26
+ }
27
+
28
+ // (i-1)과 (j-1) 번째 문자가 서로 다른 경우
29
+ else {
30
+ dp [i ][j ] = Math .max (dp [i - 1 ][j ], dp [i ][j - 1 ]);
31
+ }
32
+ }
33
+ }
34
+ System .out .println (dp [length_1 ][length_2 ]);
35
+ }
36
+ }
37
+ /*
38
+ input
39
+ ACAYKP
40
+ CAPCAK
41
+
42
+ output
43
+ 4
44
+ */
You can’t perform that action at this time.
0 commit comments