[BOJ] 1701 Cubeditor

๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

Baekjoon Online Judge - 1701๋ฒˆ Cubeditor

Cubelover๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด Whitespace์˜ ์ฝ”๋”ฉ์„ ๋„์™€์ฃผ๋Š” ์–ธ์–ด์ธ Cubelang์„ ๋งŒ๋“ค์—ˆ๋‹ค. Cubelang์„ ์ด์šฉํ•ด ์ฝ”๋”ฉ์„ ํ•˜๋‹ค๋ณด๋‹ˆ, ์ ์  ์ด ์–ธ์–ด์— ๋งž๋Š” ์ƒˆ๋กœ์šด ์—๋””ํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ์˜ค๋žœ ์‹œ๊ฐ„ ๊ณ ์ƒํ•œ ๋์— ์ƒˆ๋กœ์šด ์—๋””ํ„ฐ๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋˜์—ˆ๊ณ , ๊ทธ ์—๋””ํ„ฐ์˜ ์ด๋ฆ„์€ Cubeditor์ด๋‹ค.

ํ…์ŠคํŠธ ์—๋””ํ„ฐ๋Š” ์ฐพ๊ธฐ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์—๋””ํ„ฐ๋Š” ์ฐพ์œผ๋ ค๊ณ  ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ๋‹จ ํ•œ ๋ฒˆ๋งŒ ๋‚˜์™€๋„ ์ฐพ๋Š”๋‹ค. Cubelover๋Š” ์ด ๊ธฐ๋Šฅ์€ Cubelang์— ๋ถ€์ ํ•ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. Cubelang์—์„œ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์€ ์–ด๋–ค ๋ฌธ์ž์—ด ๋‚ด์—์„œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด ๋‘ ๋ฒˆ ์ด์ƒ ๋‚˜์˜ค๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. ์ด๋•Œ, ๋‘ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ๊ฒน์ณ๋„ ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, abcdabc์—์„œ abc๋Š” ๋‘ ๋ฒˆ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, abcd๋Š” ํ•œ ๋ฒˆ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰์ด ๋˜์ง€๋ฅผ ์•Š๋Š”๋‹ค.

์ด๋ ‡๊ฒŒ ์–ด๋–ค ๋ฌธ์ž์—ด์—์„œ ๋‘ ๋ฒˆ ์ด์ƒ ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ๋งค์šฐ ๋งŽ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ค‘์—์„œ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๊ฒƒ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์˜ˆ๋ฅผ ๋“ค์–ด, abcabcabc์—์„œ abc๋Š” ์„ธ ๋ฒˆ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜, abcabc๋„ ๋‘ ๋ฒˆ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ, abcabca๋Š” ํ•œ ๋ฒˆ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ, ๋‘ ๋ฒˆ ์ด์ƒ ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ค‘์—์„œ ๊ฐ€์žฅ ๊ธด ๊ฒƒ์€ abcabc์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ด ๋ฌธ์ž์—ด์ด ๋‹ต์ด ๋œ๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 5,000์ด๊ณ , ๋ฌธ์ž์—ด์€ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

์ถœ๋ ฅ

์ž…๋ ฅ์—์„œ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ๋‘ ๋ฒˆ์ด์ƒ ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„๋ฌธ์ž์—ด ์ค‘์—์„œ ๊ฐ€์žฅ ๊ธด ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

ํ’€์ด

ํ•ด์„ค

๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์„œ๋ธŒ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด KMP ์‹œ๋„ํ•˜์—ฌ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด ๊ธธ์ด์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

KMP์—์„œ๋Š” ๋ฌธ์ž์—ด์˜ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์ ‘๋‘์‚ฌ๋กœ ์„ค์ •ํ•˜์—ฌ ๋ฌธ์ž์—ด ์ผ์น˜ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•˜๋ฏ€๋กœ, ์„œ๋ธŒ ๋ฌธ์ž์—ด์€ ์›๋ณธ ๋ฌธ์ž์—ด์˜ ๋งจ ์•ž์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž ํ•˜๋‚˜์”ฉ ์ง€์›Œ๊ฐ€๋ฉฐ ๊ตฌ์„ฑํ•ด์•ผ ํ•œ๋‹ค.

์‹œํ–‰์ฐฉ์˜ค

์ด๋ฏธ ๋“ฑ์žฅํ•œ ์  ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ๋˜ ๋“ฑ์žฅํ–ˆ๋‹ค๋ฉด ์•ž์„œ ๋“ฑ์žฅํ•œ ์ง€์ ๋ถ€ํ„ฐ ํ˜„์žฌ๊นŒ์ง€์˜ ๋ฌธ์ž์—ด์ด, ๋ฐ˜๋ณต๋˜๋Š” ์„œ๋ธŒ ๋ฌธ์ž์—ด์ด ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ.

๋”ฐ๋ผ์„œ ์ด๋Ÿฌํ•œ ๋ฌธ์ž์—ด๋“ค๋งŒ ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„ KMP๋ฅผ ์‹œ๋„ํ–ˆ๋Š”๋ฐ ๋‘๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Œ.

  • ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ์ž๊ธฐ ์ž์‹ ์ด ๋–ผ์–ด์ ธ ๋‚˜์˜จ ๋ฐ”๋กœ ๊ทธ ์œ„์น˜๋ผ๋ฉด ์ค‘๋ณต๋˜๋Š” ๊ฒƒ์ด ์•„๋‹˜
  • ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ํ†ต์งธ๋กœ ์ผ์น˜ํ•˜์ง€ ์•Š๊ณ  ์ผ๋ถ€ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ ์ฒดํฌ๊ฐ€ ์–ด๋ ค์›€

๋”ฐ๋ผ์„œ ์‹คํŒจโ€ฆ

์ฝ”๋“œ

Java
import java.io.*;

public class BOJ_1701_Cubeditor {
	static String str;
	static char[] subChars;
	static int strLen, subLen, maxLen;
	static int[] returnPointer;
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		str = br.readLine();
		strLen = str.length();
		
		findSubStrs();
		System.out.println(maxLen);
	}

	private static void findSubStrs() {
		for (int i=0; i<strLen-1; i++) {
			subChars = str.substring(i, strLen).toCharArray();
			subLen = subChars.length;
			returnPointer = new int[subLen];
			
			makeReturnPointerArr(i);
		}
	}

	private static void makeReturnPointerArr(int s) {
		for (int i=1, j=0; i<subLen; i++) {
			while (j>0 && subChars[i]!=subChars[j]) j = returnPointer[j-1];
			if (subChars[i]==subChars[j]) {
				returnPointer[i] = ++j;
				maxLen = Math.max(maxLen, j);
			}
		}
	}

}

๐Ÿ’ก

  • ๋ญ”๊ฐ€ ์‹œํ–‰์ฐฉ์˜ค๋กœ ์‹คํŒจํ•œ ๋กœ์ง ์ž˜ ๊ตฌํ˜„ํ•˜๋ฉด ํ†ต๊ณผํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ,, ๋‹ค์‹œ ๋„์ „ํ•ด๋ด์•ผ ๊ฒ ๋‹ค.