🌞Algorithm/🔥Baekjoon

[Baekjoon] 4775_Spelling Be

뿌야._. 2025. 1. 15. 16:57
문제(출처: https://www.acmicpc.net/problem/4775)

< Spelling Be >

 

문제 풀이 

 

HashSet에 단어를 저장하고 저장된 단어가 아니라면 출력한다.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class _4775_ { // Spelling Be

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
		int n = Integer.parseInt(bf.readLine());
        
		Set<String> set = new HashSet<>();
		for (int i = 0; i < n; i++) {
			set.add(bf.readLine());
		}
        
		int idx = 1;
		ArrayList<String> list = new ArrayList<>();
		
		n = Integer.parseInt(bf.readLine());
		
		while (n > 0) {
			String str = bf.readLine();
			if (str.equals("-1")) {
				n--;
				if (list.size() > 0) {
					bw.write("Email " + (idx++) + " is not spelled correctly.\n");
					for (int i = 0; i < list.size(); i++) {
						bw.write(list.get(i) + "\n");
					}
				} else {
					bw.write("Email " + (idx++) + " is spelled correctly.\n");
				}
				list.clear();
			} else if (!set.contains(str)) {
				list.add(str);
			}
		}
		bw.write("End of Output");
		bw.flush();
	}
}
변수)
n : 단어 수
set : 단어
idx : 이메일 번호
list : 사전에 없는 단어
n : 테스트 케이스 수
str : 입력 단어

 

단어 수를 입력받는다. 단어 수만큼 단어를 입력받아 HashSet에 저장한다. 테스트 케이스 수를 입력받고 그 수만큼 다음 과정을 반복한다.

1) -1이 입력되기 전까지 입력받은 단어가 HashSet에 없다면 ArrayList에 저장한다.

2) -1이라면 list size를 확인하고 0이라면 "Email x is spelled correctly."를 출력하고 0이 아니라면 "Email x is not spelled correctly."와 list 값을 출력한다. 

 

마지막으로 "End of Output"을 출력한다.



 

'🌞Algorithm > 🔥Baekjoon' 카테고리의 다른 글

[Baekjoon] 9951_Word Extraction  (1) 2025.01.17
[Baekjoon] 11518_Morse  (0) 2025.01.16
[Baekjoon] 18294_Biodiversity  (0) 2025.01.14
[Baekjoon] 18679_Banana  (0) 2025.01.13
[Baekjoon] 11645_I’ve Been Everywhere, Man  (0) 2025.01.10