题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
const rl = require("readline").createInterface({ input: process.stdin }); const inputs = []; rl.on("line", (line) => { inputs.push(line); }).on("close", () => { const res = resolve(inputs); console.log(res); }); function resolve(inputs) { const s1 = inputs[0]; const s2 = inputs[1]; const m = s1.length, n = s2.length; const dp = Array.from(Array(m+1), ()=> Array(n + 1).fill(0)); let res = -1; for(let i = 1; i <= m; i++) { for(let j = 1; j <= n; j++) { if(s1[i - 1] === s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } res = Math.max(res, dp[i][j]); } } return res; }
子串:注意连续性