解题思路:
(1)中心扩展法
#include<iostream>
using namespace std;
pair<int,int> cal(string &s,int i,int j) {
while(i>=0 && j<s.length() && s[i]==s[j]) {
i--;
j++;
}
return {i+1,j-1};
}
int helper(string &s) {
int maxlen=0,last=0;
for(int i=0;i<s.length();i++) {
auto a = cal(s,i,i);
if(a.second-a.first>maxlen) {
maxlen=a.second-a.first;
last=a.second;
}
a = cal(s,i,i+1);
if(a.second-a.first>maxlen) {
maxlen=a.second-a.first;
last=a.second;
}
}
return maxlen+1;
}
int main() {
string s;
while(cin>>s) {
cout<<helper(s)<<endl;
}
return 0;
}