728x90
문제
https://www.acmicpc.net/problem/12904
문제 해석
처음엔 재귀 방식으로 가볍게 도전했다가 처참하게 깨졌다...
S의 길이가 999, T의 길이가 1000이기 때문에 재귀는 시간 초과가 발생한다.
해결 방법은 최대 길이가 T이기 때문에 T에서 하나씩 제거하며 결국 S와 길이가 같아졌을 때 비교하면 해결할 수 있다.
풀이
const filePath = process.platform === "linux" ? "dev/stdin" : "../test.txt";
const input = require("fs").readFileSync(filePath).toString().trim().split("\n");
function solution(input) {
let a = input[0].trim().split("");
let b = input[1].trim().split("");
let answer = 0;
while (1) {
if (a.length === b.length) {
if (isSameArray(a, b)) answer = 1;
break;
}
if (b[b.length - 1] === "A") b.pop();
else {
b.pop();
b.reverse();
}
}
function isSameArray(arr1, arr2) {
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) return false;
else continue;
}
return true;
}
return answer;
}
console.log(solution(input));
끝
728x90
'WEB > 백준 문제 풀이' 카테고리의 다른 글
백준 16637 괄호 추가하기 (node.js) (0) | 2024.06.07 |
---|---|
백준 31863 내진설계 (node.js) (0) | 2024.06.06 |
백준 17413번 단어 뒤집기 2 (node.js) (0) | 2024.05.24 |
백준 14502번 연구소 (node.js) (0) | 2024.05.23 |
백준 3190번 뱀 (node.js) (0) | 2024.05.22 |