题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
//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%3D85%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=85 #include <iostream> #include <algorithm> using namespace std; int maxlength(string s, int l, int r){ while(l>=0&&r<s.size()&&s[l]==s[r]){ l--; r++; } return r-l-1; } int main() { string s; int maxn = 0; while(cin >> s){ for(int i = 0;i<s.size();i++) maxn = max(maxn, max(maxlength(s, i, i), maxlength(s, i, i+1))); cout << maxn <<endl; } }