[백준9251_자바스크립트(javascript)] - LCS

경이·2024년 10월 20일

𝑩𝑶𝑱 (𝒋𝒔)

목록 보기
225/325

🔴 문제

LCS


🟡 Sol

const fs = require('fs');
const path = process.platform === 'linux' ? '/dev/stdin' : 'Wiki\\input.txt';
const [str1, str2] = fs.readFileSync(path, 'utf-8').trim().split('\n');

const lcs = Array.from({ length: str1.length + 1 }, () => Array(str2.length + 1).fill(0));

for (let i = 1; i <= str1.length; i++) {
  for (let j = 1; j <= str2.length; j++) {
    if (str1[i - 1] === str2[j - 1]) {
      lcs[i][j] = lcs[i - 1][j - 1] + 1;
    } else {
      lcs[i][j] = Math.max(lcs[i - 1][j], lcs[i][j - 1]);
    }
  }
}

console.log(lcs[str1.length][str2.length]);

🟢 풀이

⏰ 소요한 시간 : -

몰라서 풀이 보고 풀었다 ... ㅠㅠ
알고보니 LCS 알고리즘의 아주 기본 유형이었다.
풀이는...첨부링크로 대체한다.
그냥 개똑같아서... 나중에 알고리즘을 정리해봐야겠다.


🔵 Ref

[알고리즘] 그림으로 알아보는 LCS 알고리즘 - Longest Common Substring와 Longest Common Subsequence

profile
록타르오가르

0개의 댓글