치춘짱베리굿나이스
[백준] 12511 본문
Magicka (Small)
문제
As a wizard, you can invoke eight elements, which are the "base" elements. Each base element is a single character from {Q, W, E, R, A, S, D, F}. When you invoke an element, it gets appended to your element list. For example: if you invoke W and then invoke A, (we'll call that "invoking WA" for short) then your element list will be [W, A].
We will specify pairs of base elements that combine to form non-base elements (the other 18 capital letters). For example, Q and F might combine to form T. If the two elements from a pair appear at the end of the element list, then both elements of the pair will be immediately removed, and they will be replaced by the element they form. In the example above, if the element list looks like [A, Q, F] or [A, F, Q] at any point, it will become [A, T].
We will specify pairs of base elements that are opposed to each other. After you invoke an element, if it isn't immediately combined to form another element, and it is opposed to something in your element list, then your whole element list will be cleared.
For example, suppose Q and F combine to make T. R and F are opposed to each other. Then invoking the following things (in order, from left to right) will have the following results:
- QF → [T] (Q and F combine to form T)
- QEF → [Q, E, F] (Q and F can't combine because they were never at the end of the element list together)
- RFE → [E] (F and R are opposed, so the list is cleared; then E is invoked)
- REF → [] (F and R are opposed, so the list is cleared)
- RQF → [R, T] (QF combine to make T, so the list is not cleared)
- RFQ → [Q] (F and R are opposed, so the list is cleared)
Given a list of elements to invoke, what will be in the element list when you're done?
입력
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of a single line, containing the following space-separated elements in order:
First an integer C, followed by C strings, each containing three characters: two base elements followed by a non-base element. This indicates that the two base elements combine to form the non-base element. Next will come an integer D, followed by D strings, each containing two characters: two base elements that are opposed to each other. Finally there will be an integer N, followed by a single string containing N characters: the series of base elements you are to invoke. You will invoke them in the order they appear in the string (leftmost character first, and so on), one at a time.
Limits
- 1 ≤ T ≤ 100.
- Each pair of base elements may only appear together in one combination, though they may appear in a combination and also be opposed to each other.
- No base element may be opposed to itself.
- Unlike in the computer game Magicka, there is no limit to the length of the element list.
- 0 ≤ C ≤ 1.
- 0 ≤ D ≤ 1.
- 1 ≤ N ≤ 10.
출력
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is a list in the format "[e0, e1, ...]" where ei is the ith element of the final element list. Please see the sample output for examples.
풀이
const magicka = () => {
const fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
input.shift();
let idx = 1;
for (let i of input) {
let arr = i.split(" ");
let stack = [];
let base = arr[arr.length - 1].split("");
const combine = parseInt(arr[0]) ? arr[1] : null;
const oppose = combine
? parseInt(arr[2])
? arr[3]
: null
: parseInt(arr[1])
? arr[2]
: null;
for (let s of base) {
stack.push(s);
if (combine && stack.length > 1) {
if (
stack[stack.length - 1] + stack[stack.length - 2] ===
combine.slice(0, 2) ||
stack[stack.length - 2] + stack[stack.length - 1] ===
combine.slice(0, 2)
) {
stack = [...stack.slice(0, stack.length - 2), combine[2]];
}
}
if (oppose)
if (stack.indexOf(oppose[0]) >= 0 && stack.indexOf(oppose[1]) >= 0)
stack = [];
}
console.log(`Case #${idx}: [${stack.join(", ")}]`);
idx++;
}
};
magicka();
반성회
스택이 쓰이긴 하는데... 구현할게 좀 복잡해서 구현 카테고리도 많이 들어가는듯
stack.indexOf()
함수가 줄을 대폭 줄여주었다.. 시간제한도 넉넉해서 빡빡하게 풀 필요 없이 내장함수 막 써도 괜찮은듯함
저거없이 플래그 조정해서 풀었다가 계속 틀려가지고....
'Javascript + Typescript > 자바스크립트로 알고리즘풀기' 카테고리의 다른 글
[백준] 4949 (0) | 2022.02.08 |
---|---|
[백준] 5957 (0) | 2022.02.08 |
[백준] 18111 (0) | 2022.02.08 |
[백준] 2798 (0) | 2022.02.07 |
[백준] 15829 (0) | 2022.02.07 |