题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { const s = (await readline()).split(""); const n = s.length; // 是一个倒三角,这样节省空间 const dp = new Array(n + 1).fill(0).map((_, index) => Array(n - index + 1).fill(0)); // dp[i][j] = 1 表示从i开始,长度为j是否为回文字符串 // 单字符串都是回文字符串 for (let i = 0; i < n; i++) { dp[i][1] = 1; } let width = 1; // 单字符都是回文字符 let max = 1; while (width < n) { width++; // 当前遍历的字符长度 for (let i = 0; i < n; i++) { let j = i + width - 1; if(j >= n) { continue; } // console.log(s[i], s[j], i, j, width) if (s[i] === s[j]) { // console.log(s[i], s[j], i, j, dp[i+1][width - 2]) if(width === 2) { dp[i][2] = 1 max = 2 } else if(dp[i+1][width - 2]) { dp[i][width] = 1 max = Math.max(max, width) } } } } // dp.forEach(n => console.log(n.join(' '))) console.log(max); // 求最长的回文字符串 })();