
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 알고리즘의 아주 기본 유형이었다.
풀이는...첨부링크로 대체한다.
그냥 개똑같아서... 나중에 알고리즘을 정리해봐야겠다.
[알고리즘] 그림으로 알아보는 LCS 알고리즘 - Longest Common Substring와 Longest Common Subsequence