๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1972_๋†€๋ผ์šด ๋ฌธ์ž์—ด

๋ฟŒ์•ผ._. 2023. 10. 12. 11:59

Silver III

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1972)

< ๋†€๋ผ์šด ๋ฌธ์ž์—ด >

 

๋ฌธ์ œ ํ’€์ด 

 

0-์Œ, 1-์Œ... N-2์Œ๊นŒ์ง€ ๊ฐ๊ฐ ๊ตฌํ•œ ํ›„ ์œ ์ผํ•œ์ง€ ์œ ์ผํ•˜์ง€ ์•Š์€์ง€ ํŒ๋‹จํ•œ๋‹ค. 

 

 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.HashSet;
import java.util.Set;

public class _1972_ { // ๋†€๋ผ์šด ๋ฌธ์ž์—ด

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		String str = "";
		Set<String> set = new HashSet<>();
		boolean flag = false;

		while (!(str = bf.readLine()).equals("*")) {
			flag = false;
			for (int i = 1; i < str.length(); i++) {
				for (int j = 0; j < str.length(); j++) {
					if (j + i < str.length()) {
						String s = str.charAt(j) + "" + str.charAt(j + i);
						if (set.contains(s)) {
							flag = true;
							break;
						} else {
							set.add(s);
						}
					}
				}
				set.clear();
				if (flag)
					break;
			}
			if (flag)
				bw.write(str + " is NOT surprising.\n");
			else
				bw.write(str + " is surprising.\n");
		}
		bw.flush();
	}
}

 

Main

๋ณ€์ˆ˜)
str : ๋ฌธ์ž์—ด
set : D-์Œ ์ €์žฅ
flag : ์œ ์ผ ์—ฌ๋ถ€

 

- ๋ฌธ์ž์—ด(str) ์ž…๋ ฅ (*์•„๋‹ ๋•Œ๊นŒ์ง€ ์ž…๋ ฅ ๊ณ„์† ๋ฐ›๊ธฐ)

- D-์Œ์„ ๊ตฌํ•œ ํ›„ set์— ์žˆ๋Š” ๋ฌธ์ž์—ด์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จ

: set์— ์žˆ๋Š” ๋ฌธ์ž์—ด์ด๋ผ๋ฉด ์ข…๋ฃŒ

: set์— ์žˆ์ง€ ์•Š์€ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด ์ถ”๊ฐ€ ํ›„ ๊ณ„์†

- flag ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ •๋‹ต ์ถœ๋ ฅ