题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
#include <iostream>
#include <string>
using namespace std;
pair<int, int> expandAroundCenter(string &s, int left, int right){
while(left >= 0 && right <= s.size() && s[left] == s[right]){
left--, right++;
}
return {left + 1, right - 1};
}
string longestPalindrome(string& s) {
int n = s.size();
int begin = 0, end = 0;
for (int i = 0; i < n; ++i) {
auto [left1, right1] = expandAroundCenter(s, i, i);
auto [left2, right2] = expandAroundCenter(s, i, i + 1);
if(right1 - left1 > end - begin){
begin = left1;
end = right1;
}
if (right2 - left2 > end - begin) {
begin = left2;
end = right2;
}
}
return s.substr(begin, end - begin + 1);
}
int main() {
string s;
cin >> s;
string res = longestPalindrome(s);
cout << res.size() << endl;
return 0;
}
// 64 位输出请用 printf("%lld")

