Skip to content

Commit 64eb910

Browse files
committed
[Gold IV] Title: 비슷한 단어, Time: 1680 ms, Memory: 18128 KB -BaekjoonHub
1 parent 7b39c59 commit 64eb910

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Gold IV] 비슷한 단어 - 2179
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2179)
4+
5+
### 성능 요약
6+
7+
메모리: 18128 KB, 시간: 1680 ms
8+
9+
### 분류
10+
11+
자료 구조, 문자열, 정렬, 집합과 맵, 해시를 사용한 집합과 맵
12+
13+
### 제출 일자
14+
15+
2026년 2월 2일 02:19:21
16+
17+
### 문제 설명
18+
19+
<p>N개의 영단어들이 주어졌을 때, 가장 비슷한 두 단어를 구해내는 프로그램을 작성하시오.</p>
20+
21+
<p>두 단어의 비슷한 정도는 두 단어의 접두사의 길이로 측정한다. 접두사란 두 단어의 앞부분에서 공통적으로 나타나는 부분문자열을 말한다. 즉, 두 단어의 앞에서부터 M개의 글자들이 같으면서 M이 최대인 경우를 구하는 것이다. "AHEHHEH", "AHAHEH"의 접두사는 "AH"가 되고, "AB", "CD"의 접두사는 ""(길이가 0)이 된다.</p>
22+
23+
<p>접두사의 길이가 최대인 경우가 여러 개일 때에는 입력되는 순서대로 제일 앞쪽에 있는 단어를 답으로 한다. 즉, 답으로 S라는 문자열과 T라는 문자열을 출력한다고 했을 때, 우선 S가 입력되는 순서대로 제일 앞쪽에 있는 단어인 경우를 출력하고, 그런 경우도 여러 개 있을 때에는 그 중에서 T가 입력되는 순서대로 제일 앞쪽에 있는 단어인 경우를 출력한다.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 N(2 ≤ N ≤ 20,000)이 주어진다. 다음 N개의 줄에 알파벳 소문자로만 이루어진 길이 100자 이하의 서로 다른 영단어가 주어진다.</p>
28+
29+
### 출력
30+
31+
<p>첫째 줄에 S를, 둘째 줄에 T를 출력한다. 단, 이 두 단어는 서로 달라야 한다. 즉, 가장 비슷한 두 단어를 구할 때 같은 단어는 제외하는 것이다.</p>
32+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.io.*;
2+
3+
public class Main {
4+
5+
static int n;
6+
static String s, t;
7+
static String[] strs;
8+
public static void main(String[] args) throws Exception{
9+
preSetting();
10+
findWord();
11+
12+
System.out.println(s);
13+
System.out.println(t);
14+
}
15+
16+
private static void findWord(){
17+
int answer = 0;
18+
int compare;
19+
20+
for(int i = 0; i < n; i++){
21+
for(int j = i + 1; j <n; j++){
22+
compare = compareStrs(strs[i], strs[j]);
23+
if(answer < compare) {
24+
s = strs[i];
25+
t = strs[j];
26+
answer = compare;
27+
}
28+
}
29+
}
30+
}
31+
32+
private static int compareStrs(String s1, String s2){
33+
if(s1.equals(s2)) return 0;
34+
35+
int answer = 0;
36+
for(int i = 0; i < Math.min(s1.length(), s2.length()); i++){
37+
if(s1.charAt(i) != s2.charAt(i)) break;
38+
39+
answer++;
40+
}
41+
return answer;
42+
}
43+
private static void preSetting() throws Exception {
44+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
45+
46+
n = Integer.parseInt(br.readLine());
47+
strs = new String[n];
48+
49+
for(int i = 0; i < n; i++){
50+
strs[i] = br.readLine();
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)