본문 바로가기

baekjoon

<java> 백준 1897 토달기

https://www.acmicpc.net/problem/1897

 

1897번: 토달기

첫 줄에 사전에 등재된 단어의 수 d와, 원장님이 처음 말씀하신 단어가 주어진다. (1 ≤ d ≤ 1,000) 원장님이 처음 말씀하신 단어의 길이는 세 글자이며, 사전에 있는 단어를 말씀하셨다. 다음 d개

www.acmicpc.net


import java.io.*;
import java.util.*;
public class Main {
	static String[] word;
	static boolean[] check;
	static Queue<Integer> q = new ArrayDeque<>();
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int d = Integer.parseInt(st.nextToken());
		String s = st.nextToken();
		
		word = new String[d];
		check = new boolean[d];
		for (int i = 0; i < d; i++) {
			word[i] = br.readLine();
			if (word[i].equals(s)) {
				q.add(i);
				check[i] = true;
			}
		}
		to();
	}
	public static void to() {
		int start = 0;
		while(!q.isEmpty()) {
			start = q.poll();
			for (int i = 0; i < word.length; i++) {
				if (check[i])
					continue;
				if (word[start].length() + 1 == word[i].length()) {
					int count = 0;
					for (int j = 0, k = 0; k < word[start].length() && j < word[i].length() && count <= 1;) {//단어 비교
						if (word[start].charAt(k) != word[i].charAt(j)) {
							count++;
							j++;
						}
						else {
							k++;
							j++;
						}
					}
					if (count <= 1) {
						q.add(i);
						check[i] = true;
					}
				}
			}
		}
		System.out.println(word[start]);
	}
}

 

 


'baekjoon' 카테고리의 다른 글

<java> 백준 1068 트리  (0) 2022.10.01
<java> 백준 14502 연구소  (1) 2022.09.30
<java> 백준 1437 수 분해  (1) 2022.09.30
<java> 백준 7569 토마토  (1) 2022.09.30
<java> 백준 7576 토마토  (0) 2022.09.30