๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 2149_์•”ํ˜ธ ํ•ด๋…

๋ฟŒ์•ผ._. 2024. 4. 12. 23:00

Silver III

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

< ์•”ํ˜ธ ํ•ด๋… >

 

๋ฌธ์ œ ํ’€์ด 

 

๋งŒ์•ฝ ์ฃผ์–ด์ง„ ํ‚ค๊ฐ€ cedfba๋ผ๋ฉด ์•”ํ˜ธ๋ฌธ์„ ๊ตฌํ•  ๋•Œ ์ •๋ ฌ๋œ ํ‚ค ๊ฐ’์€ abcdef๊ฐ€ ๋˜๋ฉฐ ์•”ํ˜ธ๋ฌธ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉํ–ฅ์œผ๋กœ ์ฑ„์šด๋‹ค.

์›๋ž˜ ํ‚ค ๊ฐ’์ผ ๋•Œ ๊ฐ’์„ ์ฐพ์•„ ๋‹ค์Œ ์ˆœ์„œ๋กœ ์ฝ๋Š”๋‹ค.

 

 

 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.Arrays;

public class _2149_ { // ์•”ํ˜ธ ํ•ด๋…

	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 key = bf.readLine();
		String str = bf.readLine();

		char arr[][] = new char[(str.length() / key.length()) + 1][key.length()];
		char keyArr[] = new char[key.length()];
		char ori[] = new char[key.length()];

		for (int i = 0; i < key.length(); i++) {
			ori[i] = key.charAt(i);
			keyArr[i] = key.charAt(i);
		}

		int idx = 0;
		for (int j = 0; j < key.length(); j++) {
			for (int i = 1; i <= (str.length() / key.length()); i++) {
				arr[i][j] = str.charAt(idx++);
			}
		}

		Arrays.sort(keyArr);
		for (int i = 0; i < key.length(); i++) {
			arr[0][i] = keyArr[i];
		}

		boolean visited[] = new boolean[key.length()];

		for(int j=1; j<arr.length; j++) {
			for (int k = 0; k < key.length(); k++) {
				for (int i = 0; i < key.length(); i++) {
					if (!visited[i] && ori[k] == arr[0][i]) {
						visited[i] = true;
						bw.write(arr[j][i]);
						break;
					}
				}
			}
			for(int k=0; k<key.length(); k++) {
				visited[k]=false;
			}
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
key, str : ํ‚ค, ์•”ํ˜ธ๋ฌธ
arr : ๋ฐฐ์—ด
keyArr, ori : ์ •๋ ฌ๋œ ํ‚ค ๊ฐ’, ํ‚ค ๊ฐ’
visited : ๋ฐฉ๋ฌธ ์—ฌ๋ถ€

 

ํ‚ค์™€ ์•”ํ˜ธ๋ฌธ์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. arr ๋ฐฐ์—ด์— ์—ด ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. arr ๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ ํ–‰์—๋Š” key๊ฐ’์„ ์ •๋ ฌํ•œ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•œ๋‹ค. ์›๋ž˜ key ๊ฐ’์„ ์ˆœํšŒํ•˜๋ฉฐ arr์˜ 0๋ฒˆ์งธ ํ–‰์˜ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์•„ ์ถœ๋ ฅํ•œ๋‹ค.