Skip to content

Commit 27758d2

Browse files
authored
[20251123] BOJ / G5 / 공통 부분 문자 / 설진영
Implement solution for finding the longest common substring.
1 parent 6798673 commit 27758d2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
6+
public class Main {
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
String s1 = br.readLine();
10+
String s2 = br.readLine();
11+
12+
int n = s1.length();
13+
int m = s2.length();
14+
int[][] dp = new int[n + 1][m + 1];
15+
int maxLen = 0;
16+
17+
for (int i = 1; i <= n; i++) {
18+
for (int j = 1; j <= m; j++) {
19+
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
20+
dp[i][j] = dp[i - 1][j - 1] + 1;
21+
maxLen = Math.max(maxLen, dp[i][j]);
22+
}
23+
}
24+
}
25+
26+
System.out.println(maxLen);
27+
}
28+
}
29+
```

0 commit comments

Comments
 (0)