题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { // Write your code here let arr = []; while(line = await readline()){ arr.push(line); } if(arr[0].indexOf(arr[1]) != -1){ console.log(arr[1].length); return; } if(arr[1].indexOf(arr[0]) != -1){ console.log(arr[0].length); return; } let dp = []; let str1 = arr[0].split(''), str2 = arr[1].split(''); let max = 0; for(let i=0;i<=str1.length;i++){ dp[i] = []; for(let j=0;j<str2.length;j++){ if(i==0||j==0){ dp[i][j] = 0; }else{ if(str1[i-1] == str2[j-1]){ dp[i][j] = dp[i-1][j-1] + 1; max = Math.max(max,dp[i][j]); // 遇到相同字符 }else{ dp[i][j] = 0; } } } } console.log(max); }()