[BOJ] 13013 ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด

๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

Baekjoon Online Judge - 13013๋ฒˆ ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด

๋ฌธ์ž์—ด S์˜ i๋ฒˆ์งธ ์ ‘๋ฏธ์‚ฌ๋Š” S์˜ i๋ฒˆ์งธ ๊ธ€์ž์—์„œ ์‹œ์ž‘ํ•˜๋Š” ์ ‘๋ฏธ์‚ฌ(Suffix)์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, S = โ€œabcdeโ€์ธ ๊ฒฝ์šฐ์— 0๋ฒˆ์งธ ์ ‘๋ฏธ์‚ฌ๋Š” โ€œabcdeโ€, 3๋ฒˆ์งธ ์ ‘๋ฏธ์‚ฌ๋Š” โ€œdeโ€ ์ด๋‹ค.

S์˜ ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์€ S์˜ ๋ชจ๋“  ์ ‘๋ฏธ์‚ฌ๋ฅผ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์ด๋‹ค. ์ด๋•Œ, ๋ฐฐ์—ด์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์€ ์ ‘๋ฏธ์‚ฌ ๋ฒˆํ˜ธ์ด๊ณ , ์ •๋ ฌ์€ ์ ‘๋ฏธ์‚ฌ ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” ์ ‘๋ฏธ์‚ฌ๋กœ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, S = โ€œabcaโ€์ธ ๊ฒฝ์šฐ์— ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์€ (3, 0, 1, 2)๊ฐ€ ๋œ๋‹ค.

๊ธธ์ด๊ฐ€ N์ธ ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ทธ ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” S๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ, S์— ํฌํ•จ๋œ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N (1 โ‰ค N โ‰ค 50)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” S ์ค‘์—์„œ, S์— ํฌํ•จ๋œ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

ํ’€์ด

ํ•ด์„ค

  • ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์—๋”ฐ๋ผ ์ธ๋ฑ์Šค ์œ„์น˜์— ๋ฌธ์ž ํ•˜๋‚˜์”ฉ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์‚ฝ์ž…ํ•œ๋‹ค(A,B,Cโ€ฆ)

ex) 3012 -> bcda

  • ์šฐ์„ ์ˆœ์œ„๊ฐ€ 1์ธ ๋ฌธ์ž๋ถ€ํ„ฐ ์šฐ์„ ์ˆœ์œ„ 0๋ฒˆ ๋ฌธ์ž๋กœ ๋Œ€์น˜ํ•œ ๋‹ค์Œ ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์„ ๊ตฌํ•˜์—ฌ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋ฌธ์ž ๋Œ€์น˜

ex) bcda -> acda

  • ๋งˆ์ง€๋ง‰ ์šฐ์„ ์ˆœ์œ„ ๋ฌธ์ž๊นŒ์ง€ ์ง„ํ–‰ํ•œ ๋’ค ๋ช‡๊ฐœ์˜ ๋ฌธ์ž๊ฐ€ ๋ฐ”๋€Œ์—ˆ๋Š”์ง€ ์นด์šดํŠธ

์ฝ”๋“œ

Java
import java.io.*;
import java.util.*;

public class BOJ_13013_์ ‘๋ฏธ์‚ฌ๋ฐฐ์—ด {
	static int N, primaryCharCnt;    // ํ•„์ˆ˜๋กœ ํ•„์š”ํ•œ ๋ฌธ์ž ๊ฐœ์ˆ˜(์ตœ์ข… ์ •๋‹ต)
	static char[] chars;             // ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์—๋”ฐ๋ผ ์ธ๋ฑ์Šค๋ฅผ ๋ฌธ์ž๋กœ ๋Œ€์น˜ํ• ๋•Œ ๋ฌธ์ž๊ฐ€ ์ €์žฅ๋˜๋Š” ๋ฐฐ์—ด
	static int[] inputIndex;         // ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด
	static String stringifiedIdx;    // ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ดํ™”
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		
		primaryCharCnt = N;
		chars = new char[N];
		inputIndex = new int[N];
		stringifiedIdx = br.readLine();
		StringTokenizer st = new StringTokenizer(stringifiedIdx);
		
		// ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด ์ž…๋ ฅ
		for (int i=0; i<N; i++) {
			int idx = Integer.parseInt(st.nextToken());
			inputIndex[i] = idx;         // ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด ์ €์žฅ
			chars[idx] = (char)(i+65);   // ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์— ๋”ฐ๋ผ ๋ฌธ์ž๋กœ ๋Œ€์น˜ํ•˜์—ฌ ๋ฌธ์ž ์ €์žฅ
		}
		String str = new String(chars);  // ๋Œ€์น˜๋œ ๋ฌธ์ž๋ฅผ ๋ถ™์—ฌ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ฆ
		
		// 1๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ๋Œ€์น˜ํ•ด๋ด„
		for (int i=1; i<N; i++) {
			char frontChar = str.charAt(inputIndex[i-1]);    // ๋Œ€์น˜๋˜๋ฉด ๋  ํƒ€๊ฒŸ ๋ฌธ์ž
			char rearChar = str.charAt(inputIndex[i]);       // ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ๋ฌธ์ž. ๋Œ€์น˜๋˜์–ด์•ผํ•  ๋ฌธ์ž
			
			// ์šฐ์„ ์ˆœ์œ„๊ฐ€ 
			String sortedIdx = sortSubString(str.replace(rearChar, frontChar));
			
			if (stringifiedIdx.equals(sortedIdx)) {
				str = str.replace(rearChar, frontChar);
				primaryCharCnt--;
			}
		}
		
		System.out.println(primaryCharCnt);
	}
	
	private static String sortSubString(String s) {
		ArrayList<String> bf = new ArrayList<String>();
		ArrayList<String> at = new ArrayList<String>();
		
		for (int size=s.length(), i=0; i<size; i++) {
			bf.add(s.substring(i, size));
			at.add(s.substring(i, size));
		}
		Collections.sort(at);
		
		StringBuilder sb = new StringBuilder();
		for (String str : at) sb.append(bf.indexOf(str)).append(" ");
		sb.setLength(sb.length()-1);
		
		return sb.toString();
	}
}

๐Ÿ’ก

  • ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๋Œ€์น˜ํ• ๋•Œ๋งˆ๋‹ค ๋‹ค์‹œ ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด์„ ๊ตฌํ•ด ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ตฌํ•˜๋ฏ€๋กœ ํšจ์œจ์ด ๋งค์šฐ ์ข‹์ง€ ์•Š์•„๋ณด์ž„..
  • ์Šคํ„ฐ๋””๋•Œ ๋‹ค๋ฅธ ๋ถ„๋“ค ํ’€์ด๋ณด๋ฉด์„œ ๋” ์ข‹์€ ํ’€์ด๊ฐ€ ์žˆ๋‹ค๋ฉด ์—…๋ฐ์ดํŠธํ• ๊ฒƒ!