Skip to content

Commit b2dde26

Browse files
committed
[Silver II] Title: 비슷한 단어, Time: 100 ms, Memory: 14200 KB -BaekjoonHub
1 parent 47cd35f commit b2dde26

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [Silver II] 비슷한 단어 - 2607
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2607)
4+
5+
### 성능 요약
6+
7+
메모리: 14200 KB, 시간: 100 ms
8+
9+
### 분류
10+
11+
구현, 문자열
12+
13+
### 제출 일자
14+
15+
2026년 3월 7일 00:04:49
16+
17+
### 문제 설명
18+
19+
<p>영문 알파벳 대문자로 이루어진 두 단어가 다음의 두 가지 조건을 만족하면 같은 구성을 갖는다고 말한다.</p>
20+
21+
<ol>
22+
<li>두 개의 단어가 같은 종류의 문자로 이루어져 있다.</li>
23+
<li>같은 문자는 같은 개수 만큼 있다.</li>
24+
</ol>
25+
26+
<p>예를 들어 "DOG"와 "GOD"은 둘 다 'D', 'G', 'O' 세 종류의 문자로 이루어져 있으며 양쪽 모두 'D', 'G', 'O' 가 하나씩 있으므로 이 둘은 같은 구성을 갖는다. 하지만 "GOD"과 "GOOD"의 경우 "GOD"에는 'O'가 하나, "GOOD"에는 'O'가 두 개 있으므로 이 둘은 다른 구성을 갖는다.</p>
27+
28+
<p>두 단어가 같은 구성을 갖는 경우, 또는 한 단어에서 한 문자를 더하거나, 빼거나, 하나의 문자를 다른 문자로 바꾸어 나머지 한 단어와 같은 구성을 갖게 되는 경우에 이들 두 단어를 서로 비슷한 단어라고 한다.</p>
29+
30+
<p>예를 들어 "DOG"와 "GOD"은 같은 구성을 가지므로 이 둘은 비슷한 단어이다. 또한 "GOD"에서 'O'를 하나 추가하면 "GOOD" 과 같은 구성을 갖게 되므로 이 둘 또한 비슷한 단어이다. 하지만 "DOG"에서 하나의 문자를 더하거나, 빼거나, 바꾸어도 "DOLL"과 같은 구성이 되지는 않으므로 "DOG"과 "DOLL"은 비슷한 단어가 아니다.</p>
31+
32+
<p>입력으로 여러 개의 서로 다른 단어가 주어질 때, 첫 번째 단어와 비슷한 단어가 모두 몇 개인지 찾아 출력하는 프로그램을 작성하시오.</p>
33+
34+
### 입력
35+
36+
<p>첫째 줄에는 단어의 개수가 주어지고 둘째 줄부터는 한 줄에 하나씩 단어가 주어진다. 모든 단어는 영문 알파벳 대문자로 이루어져 있다. 단어의 개수는 100개 이하이며, 각 단어의 길이는 10 이하이다.</p>
37+
38+
### 출력
39+
40+
<p>입력으로 주어진 첫 번째 단어와 비슷한 단어가 몇 개인지 첫째 줄에 출력한다.</p>
41+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.io.*;
2+
3+
public class Main {
4+
5+
static String[] words;
6+
static int n, cnt;
7+
static int[][] alphas;
8+
9+
public static void main(String[] args) throws Exception {
10+
preSetting();
11+
int answer = 0;
12+
13+
for (int i = 1; i < n; i++) answer += wordChecker(i, alphas[i]);
14+
System.out.println(answer);
15+
}
16+
17+
private static int wordChecker(int idx, int[] alpha) {
18+
int diff = cnt - words[idx].length();
19+
int category = 1;
20+
int sub = 0;
21+
22+
if (1 < Math.abs(diff)) return 0;
23+
if (diff == 1 || diff == -1) category = 0;
24+
25+
for (int j = 0; j < 26; j++) {
26+
sub += Math.abs(alphas[0][j] - alpha[j]);
27+
}
28+
29+
if(category == 0 && 1 < sub) return 0;
30+
if(category == 1 && 2 < sub) return 0;
31+
32+
return 1;
33+
}
34+
35+
private static void preSetting() throws Exception {
36+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
37+
n = Integer.parseInt(br.readLine());
38+
alphas = new int[n][26];
39+
words = new String[n];
40+
41+
int idx;
42+
char[] charWord;
43+
for (int i = 0; i < n; i++) {
44+
words[i] = br.readLine();
45+
charWord = words[i].toCharArray();
46+
47+
for (int j = 0; j < charWord.length; j++) {
48+
idx = charWord[j] - 'A';
49+
alphas[i][idx]++;
50+
}
51+
}
52+
cnt = words[0].length();
53+
}
54+
}

0 commit comments

Comments
 (0)