๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/1544)
< ์ฌ์ดํด ๋จ์ด >
๋ฌธ์ ํ์ด
๋จ์ด ๊ธธ์ด๊ฐ ์ผ์นํ๋ค๋ฉด ์๊ณ๋ฐฉํฅ์ผ๋ก ์ฝ์์ ๋ ๊ฐ์ ๋จ์ด์ธ์ง ํ์ธํ๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class _1544_ { // ์ฌ์ดํด ๋จ์ด
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bf.readLine());
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
String str = bf.readLine();
boolean flag = false;
for (int j = 0; j < list.size(); j++) {
if (list.get(j).length() != str.length()) {
continue;
} else {
Queue<Integer> queue = new LinkedList<>();
for (int x = 0; x < list.get(j).length(); x++) {
if (list.get(j).charAt(x) == str.charAt(0)) {
queue.add(x);
}
}
while (!queue.isEmpty()) {
int start = queue.poll();
boolean check = false;
for (int x = start; x < list.get(j).length(); x++) {
if (str.charAt(x - start) != list.get(j).charAt(x)) {
check = true;
break;
}
}
if (!check) {
check = false;
for (int x = 0; x < start; x++) {
if (str.charAt(str.length() - start + x) != list.get(j).charAt(x)) {
check = true;
break;
}
}
if (!check) {
flag = true;
break;
}
}
}
}
if (flag) {
break;
}
}
if (!flag) {
list.add(str);
}
}
System.out.println(list.size());
}
}
๋ณ์)
n : ๋จ์ด์ ๊ฐ์
list : ์๋ก ๋ค๋ฅธ ๋จ์ด
str : ์ ๋ ฅ๋ฐ์ ๋จ์ด
flag : ์๋ก์ด ๋จ์ด์ธ์ง ๊ฐ์ ๋จ์ด์ธ์ง ํ๋ณ
queue : ์์ ์์น ์ ์ฅํ๋ Queue
start : ์์ ์์น
check : ๊ฐ์ ๋จ์ด์ธ์ง ํ๋ณ
๋จ์ด ๊ฐ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ๋จ์ด ๊ฐ์๋งํผ ๋จ์ด๋ฅผ ์ ๋ ฅ๋ฐ์ผ๋ฉด์ ๊ฐ์ ๋จ์ด์ธ์ง ๋ค๋ฅธ ๋จ์ด์ธ์ง ํ๋จํ๋ค.
1) ๋จ์ด ์ ๋ ฅ
2) list๋ฅผ ํ์
2-1) ๊ธธ์ด๊ฐ ๊ฐ์ง ์๋ค๋ฉด ์ฌ์ดํด ๋จ์ด๋ฅผ ํ๋จํ ํ์๊ฐ ์์ผ๋ฏ๋ก ๋๊น
2-2) ๊ธธ์ด๊ฐ ๊ฐ๋ค๋ฉด list ๋จ์ด๋ฅผ ํ์ -> ์ ๋ ฅ๋ฐ์ ๋จ์ด์ ์ฒซ ๋ฒ์งธ ๊ฐ๊ณผ list ๋จ์ด ์ค์์ ์ผ์นํ๋ ์ธ๋ฑ์ค๋ฅผ ์ฐพ์ queue์ ์ ์ฅ -> queue๊ฐ ๋น ๋๊น์ง ๋ฐ๋ณต -> ์ฌ์ดํด์ ๋๋ฉฐ ๊ฐ์ ๋จ์ด์ธ์ง ํ๋จ
3) ๊ฐ์ ๋จ์ด๊ฐ ์๋๋ผ๋ฉด list์ ๋จ์ด ์ถ๊ฐ
์ต์ข list์ ๊ธธ์ด๋ฅผ ์ถ๋ ฅํ๋ค.

'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 11504_๋๋ ค ๋๋ ค ๋๋ฆผํ! (0) | 2024.05.27 |
---|---|
[Baekjoon] 2891_์นด์ฝ๊ณผ ๊ฐํ (0) | 2024.05.24 |
[Baekjoon] 3568_iSharp (0) | 2024.05.22 |
[Baekjoon] 2942_ํผ๊ฑฐ์จ๊ณผ ์ฌ๊ณผ (0) | 2024.05.20 |
[Baekjoon] 3018_์บ ํํ์ด์ด (0) | 2024.05.17 |