치춘짱베리굿나이스
[백준] 5966 본문
Cow Cotillion
문제
The cow cotillion, a fancy dance every spring, requires the cows (shown as ">") and bulls (shown as "<") to bow to each other during a dance. Schematically, one properly bowing pair of cattle is shown like this: "><". Sometimes another pair of cattle will sashay between a pair of bowing cows: "> >< <".
In fact, sometimes a larger number of cows will mix it up on the dance floor: "> >< < ><" (this includes a second set of bowing cows on the right). Complex arrangements can be perfectly legal dance formations:
> > > >< < >< < >< >< >< <
| | | -- | -- | -- -- -- |
| | ------ | |
| ------------- |
--------------------------
Farmer John notices that a stray heifer sometimes sneaks into a group and unbalances it: "> >< < <><". This is strictly forbidden; FJ wants to punish the interlopers.
FJ has copied down records of as many as 500 cows participating in dance lines and wonders if the dance line is properly balanced (i.e., all of the cattle can be paired off in at least one way as properly bowing pair by pair). He copied only the direction each cow was bowing without any extra spaces to help determine which cow was bowing to which bull, strings like this rendition of the illegal example from the previous paragraph: ">><<<><". He wants you to write a program to tell him if the dance line is legal.
FJ has N (1 <= N <= 1,000) pattern recordings P_i comprising just the characters '>' and '<' with varying length K_i (1 <= K_i <= 200). Print "legal" for those patterns that include proper pairs of bowing cows and "illegal" for those that don't.
입력
- Line 1: A single integer: N
- Lines 2..N+1: Line i contains an integer followed by a space and a string of K characters '>' and '<': K_i and P_i
출력
- Lines 1..N: Line i contains either the word "legal" or "illegal" (without the quotes, of course) depending on whether the input has a legal bowing configuration.
풀이
const cow = () => {
let input = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n");
input.shift();
input = input.map((n) => {
return n.split(" ")[1];
});
let ans = [];
for (let str of input) {
let stack = [];
for (let c of str) {
if (stack.length > 0 && c === "<" && stack[stack.length - 1] === ">")
stack.pop();
else if (c === "<" || c === ">") stack.push(c);
}
if (stack.length) ans.push("illegal");
else ans.push("legal");
}
console.log(ans.join("\n"));
};
cow();
반성회
어제 403 에러로 채점기가 안 돌아서 오늘 채점했다
괄호짝짓기 문제랑 똑같음 내용자체는
'Javascript + Typescript > 자바스크립트로 알고리즘풀기' 카테고리의 다른 글
[백준] 11866 (0) | 2022.02.08 |
---|---|
[백준] 9012 (0) | 2022.02.08 |
[백준] 23253 (0) | 2022.02.08 |
[백준] 1018 (0) | 2022.02.08 |
[백준] 1181 (0) | 2022.02.08 |