题解 | #公共子串计算#
公共子串计算
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;
let arr = [];
void (async function () {
// Write your code here
while ((line = await readline())) {
let re = /[a-z]+/;
// console.log()
arr.push(line.match(re)[0]);
if (arr.length == 2) {
let sortList = [];
let longList = [];
if (arr[0].length <= arr[1].length) {
sortList = arr[0];
longList = arr[1];
} else {
sortList = arr[1];
longList = arr[0];
}
let shortlen = sortList.length;
let longLen = longList.length;
let max = 0;
for (let i = 0; i < shortlen; i++) {
let index = i;
let endindex = 1;
let str = sortList.substring(index, index + endindex);
let re = new RegExp(str);
while (re.test(longList) && index + endindex <= shortlen) {
if (str.length > max) {
max = str.length;
}
endindex++;
str = sortList.substring(index, index + endindex);
re = new RegExp(str);
}
}
console.log(max);
}
}
})();