leetcode上有一道类似的
https://www.acwing.com/activity/content/code/content/339912/
#include<iostream>
using namespace std;
string longestPalindrome(string s) {
string res;
for (int i = 0; i < s.size(); i ++ ) {
int l = i - 1, r = i + 1;
while (l >= 0 && r < s.size() && s[l] == s[r]) l --, r ++ ;
if (res.size() < r - l - 1) res = s.substr(l + 1, r - l - 1);
l = i, r = i + 1;
while (l >= 0 && r < s.size() && s[l] == s[r]) l --, r ++ ;
if (res.size() < r - l - 1) res = s.substr(l + 1, r - l - 1);
}
return res;
}
int main(){
string s;
getline(cin,s);
cout<<longestPalindrome(s).size()<<endl;
return 0;
}