๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 17609_ํšŒ๋ฌธ

๋ฟŒ์•ผ._. 2023. 12. 18. 14:26

Gold V

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

< ํšŒ๋ฌธ >

 

๋ฌธ์ œ ํ’€์ด 

 

๋ฌธ์ž์—ด์˜ ๋งจ ์ฒ˜์Œ๊ณผ ๋์„ ์‹œ์ž‘์œผ๋กœ ์ค‘๊ฐ„๊นŒ์ง€ ๋ณด๋ฉด์„œ ํšŒ๋ฌธ์ธ์ง€ ํ™•์ธํ•œ๋‹ค. 

 

* ์™ผ์ชฝ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ–ˆ์„ ๋•Œ, ์˜ค๋ฅธ์ชฝ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ–ˆ์„ ๋•Œ ๋‘˜ ๋‹ค ํšŒ๋ฌธ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค๋ฉด ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ ๋‹ค ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class _17609_ { // ํšŒ๋ฌธ
	static String str;
	static boolean remove, check, result;

	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 t = Integer.parseInt(bf.readLine());

		for (int i = 0; i < t; i++) {
			str = bf.readLine();

			int start = 0, end = str.length() - 1;
			remove = false;
			check = false;
			result = false;

			check(start, end);

			if (check) {
				bw.write("2\n");
			} else if (remove) {
				bw.write("1\n");
			} else if (result) {
				bw.write("0\n");
			}
		}
		bw.flush();
	}

	private static void check(int start, int end) {
		while (start != end) {
			if (result) {
				break;
			}
			if (str.charAt(start) == str.charAt(end)) {
				if (start + 1 > end - 1 || start + 1 == end - 1) {
					result = true;
					break;
				}
				start++;
				end--;
			} else if (!remove) {
				if (str.charAt(start + 1) == str.charAt(end) && str.charAt(start) == str.charAt(end - 1)) {
					remove = true;
					if (start + 1 == end) {
						break;
					}
					check(start + 1, end);
					if (check) {
						check = false;
					}
					check(start, end - 1);
				}
				if (str.charAt(start) == str.charAt(end - 1)) {
					remove = true;
					start++;
					end -= 2;
				} else if (str.charAt(start + 1) == str.charAt(end)) {
					remove = true;
					start += 2;
					end--;
				} else {
					check = true;
					break;
				}
			} else {
				check = true;
			}
			if (result) {
				break;
			}
			if (check) {
				break;
			}
		}
	}
}
๋ณ€์ˆ˜)
t : ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜
str : ๋ฌธ์ž์—ด
start, end : ์ธ๋ฑ์Šค
remove : ํ•œ ๋ฌธ์ž ์‚ญ์ œ ์—ฌ๋ถ€
check : ํšŒ๋ฌธ, ์œ ์‚ฌํšŒ๋ฌธ ์•„๋‹Œ ์—ฌ๋ถ€
result : ํšŒ๋ฌธ ์—ฌ๋ถ€

 

๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ํšŒ๋ฌธ์ธ์ง€ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•ด check ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

๋งŒ์•ฝ ๋ฌธ์ž์—ด์˜ start ์ธ๋ฑ์Šค ์œ„์น˜์˜ ๋ฌธ์ž์™€ end ์ธ๋ฑ์Šค ์œ„์น˜์˜ ๋ฌธ์ž๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด start+1, end-1์„ ํ•œ๋‹ค. ๋งŒ์•ฝ stat ์ธ๋ฑ์Šค ์œ„์น˜์˜ ๋ฌธ์ž์™€ end ์ธ๋ฑ์Šค ์œ„์น˜์˜ ๋ฌธ์ž๊ฐ€ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ํ•œ ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ•ด์„œ ํšŒ๋ฌธ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ์•„์ง ํ•œ ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์™ผ์ชฝ ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ–ˆ์„ ๋•Œ์™€ ์˜ค๋ฅธ์ชฝ ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ–ˆ์„ ๋•Œ ์–ด๋Š ์ชฝ์ด ํšŒ๋ฌธ์ผ์ง€๋ฅผ ํŒ๋‹จํ•ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ์™ผ์ชฝ ๋ฌธ์ž๋ฅผ ์ง€์›Œ๋„ ์˜ค๋ฅธ์ชฝ ๋ฌธ์ž์™€ ๊ฐ™๊ณ , ์˜ค๋ฅธ์ชฝ ๋ฌธ์ž๋ฅผ ์ง€์›Œ๋„ ์™ผ์ชฝ ๋ฌธ์ž์™€ ๊ฐ™๋‹ค๋ฉด ์™ผ์ชฝ์„ ์ง€์› ์„ ๋•Œ, ์˜ค๋ฅธ์ชฝ์„ ์ง€์› ์„ ๋•Œ ๋‘˜ ๋‹ค ํ™•์ธํ•ด์•ผ ํ•˜๋ฏ€๋กœ check ํ•จ์ˆ˜๋ฅผ ๊ฐ๊ฐ ํ˜ธ์ถœํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ํ•œ์ชฝ๋งŒ ์ง€์šด๋‹ค. ๋ชจ๋“  ๊ฒฝ์šฐ์— ํ•ด๋‹นํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํšŒ๋ฌธ๊ณผ ์œ ์‚ฌ ํšŒ๋ฌธ์ด ์•„๋‹ˆ๋ฏ€๋กœ check๋ฅผ true๋กœ ๋ฐ”๊พธ๊ณ  ์ข…๋ฃŒํ•œ๋‹ค. ์ค‘๊ฐ„๊นŒ์ง€ ํ™•์ธํ•ด์„œ ํšŒ๋ฌธ์ด ๋งž๋‹ค๋ฉด result๋ฅผ true๋กœ ๋ฐ”๊พธ๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

 

check๊ฐ€ true๋ผ๋ฉด ํšŒ๋ฌธ๊ณผ ์œ ์‚ฌํšŒ๋ฌธ์ด ์•„๋‹ˆ๋ฏ€๋กœ 2๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  remove๊ฐ€ true๋ผ๋ฉด ์œ ์‚ฌํšŒ๋ฌธ์ด๋ฏ€๋กœ 1์„ ์ถœ๋ ฅ, result๊ฐ€ true๋ผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.