给定一个仅包含小写字母的字符串,求它的最长回文子串的长度。
所谓回文串,指左右对称的字符串。
所谓子串,指一个字符串删掉其部分前缀和后缀(也可以不删)的字符串
数据范围:字符串长度
进阶:时间复杂度:,空间复杂度:
有没有大神看一下怎么回事, 只能通过部分用例, 孩子都急哭了 const rl = require("readline").createInterface({ input: process.stdin, output: process.stdout, }); rl.on("line", (data) => { function a(data) { let arr = data.trim().split(""); let len = data.length; let lastlen = 0; //使用对象存储已经判断过的子列序号 let newarr = {}; //判断是不是回文数,其中a,b为arr中的子列起始位置 function isHw(arr, a, b) { if (arr.slice(a, b).reverse().join("") == arr.slice(a, b).join("")) { return true; } return false; } //子列处理函数 function resolve(arr, a, b) { if (newarr[`${a}${b}`] != undefined) { return false; } newarr[`${a}${b}`] = lastlen; if (a >= b) { return false; } if (isHw(arr, a, b)) { lastlen = Math.max(lastlen, b - a); return false; } else { resolve(arr, a, b - 1); resolve(arr, a + 1, b); } } resolve(arr, 0, len); console.log(lastlen); } a(data); })
function toManacherString(str) { let a = ['#']; for (let i=0;i<str.length;i++) { a.push(str[i]); a.push('#'); } return a; } function manacher(str) { let a = toManacherString(str); let ra = Array(a.length).fill(0); let c = -1, r = -1; let max = Number.MIN_SAFE_INTEGER; debugger; for (let i = 0; i < a.length; i++) { if (r > i) { let pl = 2 * c - i; if (pl - ra[pl] > c - r) { ra[i] = ra[pl]; continue; } else if (pl - ra[pl] < c - r) { ra[i] = r - i + 1; continue; } else { ra[i] = r - i + 1; } } while (i + ra[i] < ra.length && i - ra[i] > -1) { if (a[i + ra[i]] === a[i - ra[i]]) { ra[i]++; } else { break; } } if (i + ra[i] > r) { r = i + ra[i] - 1; c = i; } if (ra[i] > max) { max = ra[i]; } } return max - 1; } let input =readline(); print(manacher(input))
// 中心扩展法,分长度为奇数和偶数两种情况,取较长的一个 let line = undefined; while(line = readline()) { console.log(longestPalindrome(line)); } function expandAroundCenter (str, left, right) { let l = left; let r = right; while(l >= 0 && r < str.length && str.charAt(l) === str.charAt(r)){ l = l - 1; r = r + 1; } return { left: l + 1, right: r - 1 } } function longestPalindrome(s) { if (s === null || s.length === 0) return 0; let start = 0; let end = 0; for (let i = 0, len = s.length; i < len; i++) { let oddLen = expandAroundCenter(s, i, i); // 奇数 let evenLen = expandAroundCenter(s, i, i+1); // 偶数 let isLonger = oddLen.right - oddLen.left > evenLen.right - evenLen.left; let maxLen = isLonger ? oddLen : evenLen; if (maxLen.right - maxLen.left > end - start) { start = maxLen.left; end = maxLen.right; } } return end - start + 1; }