题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507?tpId=37&tqId=21308&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26pageSize%3D50%26search%3D%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
#include <iostream>
using namespace std;
int main() {
string str;
getline (cin, str);
int count1 = 0, count2 = 1, maxn = 0, pos = 1;
for (int i = 0; i < str.length(); i++) { //abcba型对称回文串的计算
while (str[i - pos] == str[i + pos]) {
count2 += 2;
pos += 1;
maxn = max(count2, maxn);
if (str[i - pos] != str[i + pos]) {
count2 =1;
pos =1;
break;
}
}
while (str[i] == str[i + pos]) { //abba型对称回文串的计算
count1 += 2;
pos += 2;
maxn = max(count1, maxn);
i -= 1;
if (str[i] != str[i + pos] || i <0) {
i += (count1 / 2 + 1);
count1 = 0;
pos = 1;
}
}
}
cout << maxn << endl;
}

