题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
#include <bits/stdc++.h> using namespace std; bool check(string s, int l, int r) { for (; l < r; l++, r--) if (s[l] != s[r]) return false; return true; // 这里我们判断一个字符串是不是回文串 } signed main() { string s; cin >> s; int maxx = INT_MIN; // 这个maxx是我们的最长回文子串的一个长度 for (int i = 0; i < s.size(); i++) { // 我们第一层循环枚举的是我们这个字符串的一个起点的位置 for (int j = i; j < s.size(); j++) { // 我们的第二层循环是用来循环我们的结尾的位置是在哪里的 if (check(s, i, j)) { maxx = max(maxx, j - i + 1); // 如果我们当前的这个字符串是回文串,我们更新一下最大的长度 } } } cout << maxx << "\n"; return 0; }