https://www.acmicpc.net/problem/2195
로직
- 그리디 알고리즘
- 만들고자 하는 문자열(P)에서 맨앞문자를 시작지점으로 고정해두고, 끝점을 한칸씩 이동하며 잘라진 문자를 한번에 복사 가능 한지 확인
- 한번에 복사 불가능 할 경우, 복사 횟수를 늘리고 문자열의 시작지점을 해당 지점으로 변경 후 다시 확인
const fs = require("fs");
const URI = process.platform === 'linux'?'dev/stdin':'./2195.txt';
const inputs = fs.readFileSync(URI).toString().trim().split('\n');
const S = inputs.shift();
const P = inputs.shift();
let ans = 1;
let idx = 0;
for (let i = 0; i< P.length; i++){
if (S.indexOf(P.slice(idx, i+1)) > -1) continue
idx = i;
ans ++;
}
console.log(ans);
// const alphaSet = new Set();
// for (let i = 0; i < S.length; i++){
// for (let j = i+1; j <= S.length; j++){
// alphaSet.add(S.slice(i,j));
// }
// }
// let ans = 0;
// let idx = 0;
// while (idx < P.length){
// let nextIdx = idx+1;
// for (let end = idx+1; end <= P.length; end ++){
// const temp = P.slice(idx,end);
// if (alphaSet.has(temp)) nextIdx = end;
// else break;
// }
// idx = nextIdx;
// ans ++
// }
// console.log(ans);
'알고리즘 문제 풀이 > Javascript' 카테고리의 다른 글
javascript (nodeJS) 백준 2302 극장 좌석 (0) | 2024.04.16 |
---|---|
javascript (nodeJS) 백준 23083 꿀벌 승연이 (0) | 2024.04.12 |
Javascript 백준 2075 N번째 큰 수 (0) | 2024.04.01 |
javascript 백준 11663 선분 위의 점 (0) | 2024.03.29 |
Javascript 백준 19941 햄버거 분배 (0) | 2024.03.28 |