2 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 1


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/1283

image

image


๐Ÿ“Œ ํ’€์ด

์ด๊ฒƒ๋„ ์ง€๊ธˆ๊นŒ์ง€ ์ญ‰ ํ’€์–ด์˜จ ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ตฌํ˜„๊ณผ ๊ด€๋ จ๋œ ๋ฌธ์ œ์˜€๋‹ค.
ํ•˜์ง€๋งŒ ์ด ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๋ฐฐ์—ด์˜ ํ•œ๊ณ„์— ๋Œ€ํ•ด์„œ ๋Š๊ผˆ์œผ๋ฉฐ, ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์„ ๊ฐ€์ง€๊ฒŒ ๋˜์—ˆ๋‹ค.
๋“ค์–ด์˜ค๋Š” OPTION์˜ ๊ฐœ์ˆ˜๋Š” ๋ฌธ์ œ๊ฐ€ ์•ˆ๋˜์ง€๋งŒ, OPTION๋งˆ๋‹ค ๊ธ€์ž ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์„œ ๋™์ ํ• ๋‹น์˜ ํ•„์š”์„ฑ์„ ์ ˆ์‹คํžˆ ๋Š๊ผˆ๋‹ค.

์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋ฐ›์€ String์„ ๋”ฐ๋กœ ๋ฐฐ์—ด์— ๋‹ด๊ฑฐ๋‚˜ ํ•˜์ง€ ์•Š๊ณ , ๋‹จ์ˆœ ๋น„๊ต๋ฅผ ํ†ตํ•ด์„œ pos๊ฐ’์„ ์•Œ์•„๋‚ด์–ด printํ•˜๊ธฐ์ „ ์ตœ์ข… result๊ฐ’์— ์ถ”๊ฐ€ํ• ๋•Œ, pos๊ฐ’์— ์žˆ๋Š” ๋ฌธ์ž์˜ ์–‘์˜†์— []๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„์„ ํ•˜์˜€๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ hotkeys๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ , ์ด hotkeys์™€ word๋ฅผ ๋น„๊ตํ•˜์—ฌ, ์—†์œผ๋ฉด ๋‚จ์€ ๋ชจ๋“  ํ•จ์ˆ˜๋“ค์„ skip์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•˜์—ฌ ์Šคํ‚ตํ•˜๊ฒŒ ํ•˜์˜€๋‹ค.
์ฒซ๋ฒˆ์งธ ๋‹จ์–ด์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ flag๋ผ๋Š” booleanํƒ€์ž…์˜ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๊ธฐ๋ณธ์ ์œผ๋กœ true๋กœ ์ƒ์„ฑ๋˜๊ณ , ํ•œ๋ฒˆ ๋ฌธ์ž๋ฅผ ๋ฝ‘์•„๋‚ผ๋•Œ ๊ณต๋ฐฑ์ด ์•„๋‹ˆ๋ฉด false๋กœ ๋ฐ”๊พธ๊ณ , ๊ณต๋ฐฑ์ด๋ฉด true๋กœ ๋ฐ”๊พธ๋Š” ์‹์œผ๋กœ ๋„์›Œ์“ฐ๊ธฐ ์ดํ›„ ์ฒซ ๊ธ€์ž๋ฅผ ์•Œ์•„๋‚ด์—ˆ๋‹ค.


๐Ÿ“Œ Code

package Implements;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class N1283 {
    public static List<String> hotkeys;
    public static String result = "";

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter((System.out)));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int num = Integer.parseInt(st.nextToken());

        hotkeys = new ArrayList<String>();

        for (int i = 0; i < num; i++) {
            st = new StringTokenizer(br.readLine());
            MakeHotkey(st);
        }

        System.out.println(result.subSequence(0, result.length() - 1));
    }

    public static void MakeHotkey(StringTokenizer st) {
        String option = "";
        while (st.hasMoreTokens()) {
            option += st.nextToken();
            if (st.hasMoreTokens()) {
                option += " ";
            }
        }
        boolean flag = true;
        boolean skip = false;
        int pos = -1;
        for (String word : option.split("")) {
            if (!skip) {
                pos += 1;
                if (flag) {
                    flag = false;
                    if (!hotkeys.contains(word.toUpperCase())) {
                        hotkeys.add(word.toUpperCase());
                        skip = true;
                    }
                }
                if (word.equals(" ")) {
                    flag = true;
                }
            }
        }
        if (!skip) {
            pos = -1;
        }

        for (String word : option.split("")) {
            if (!skip) {
                pos += 1;
                if (!word.equals(" ")) {
                    if (!hotkeys.contains(word.toUpperCase())) {
                        hotkeys.add(word.toUpperCase());
                        skip = true;
                    }
                }
            }
        }
        if (skip) {
            InsertOption(option, pos);
        } else {
            InsertOption(option, -10);
        }
    }

    public static void InsertOption(String option, int pos) {
        if (pos != -10) {
            StringBuffer sb = new StringBuffer();
            sb.append(option);
            sb.insert(pos + 1, "]");
            sb.insert(pos, "[");

            result += sb + "\n";
        } else {
            result += option + "\n";
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.


๐Ÿ“Œ ์ถ”๊ฐ€ ์˜ˆ์ œ

[Input]
5
AAA AAA AAA AAA
AAA BCC CCC DDD
AAA BBB CCC DDD
AGF AAA AAA AAA
AAA AAA AAA AAA
[Input]
12
Aab
Z bcd
a hw rf
h aH
BB hRf
haa aje
AHHAB Bz
ah zb a a k
ABB III
ajjeEt hhq
a j h h e o
apP po



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜