[백준 9251번] 다이나믹 프로그래밍 - LCS

김민지·2023년 7월 5일
0

냅다 시작 백준

목록 보기
50/118

✨ 문제 ✨

✨ 정답 ✨

const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./예제.txt";
let input = fs.readFileSync(filePath).toString().trim();

// const fs = require('fs'); 
// let input = fs.readFileSync('/dev/stdin').toString().trim();

let A=input.split('\n')[0]
let B=input.split('\n')[1]
let LCS=[]


const lcsArray = Array.from(Array(A.length + 1), () => new Array(B.length + 1).fill(0));

const check=(A, B)=>{
    for (let i=0;i<A.length;i++){
        for (let j=0;j<B.length;j++){
            if (A[i]===B[j]){
                lcsArray[i+1][j+1]=lcsArray[i][j]+1;
            }else{
                lcsArray[i+1][j+1]=Math.max(lcsArray[i][j+1], lcsArray[i+1][j])
            }
        }
    }   
console.log(lcsArray)
    return lcsArray[A.length][B.length]
}



console.log(check(A,B))

🧵 참고한 정답지 🧵

💡💡 기억해야 할 점 💡💡

dp라고 해서 무작정 dp[n]과 dp[n+1]의 관계만을 찾으려 했는데 그게 아닌 것 같다.

profile
이건 대체 어떻게 만든 거지?

0개의 댓글