中心扩展法。
typedef pair<int, int> PII; #define x first #define y second class Solution { public: PII expand(const string& A, int left, int right) { while (left >= 0 && right < A.length() && A[left] == A[right]) -- left, ++ right; return make_pair(left + 1, right - 1); } int getLongestPalindrome(string A, int n) { if (A.empty()) return 0; int max_length = 0, st = 0, ed = 0; for (int i = 0; i < A.length(); ++ i) { PII p1 = expand(A, i, i); PII p2 = expand(A, i, i + 1); if (p1.y - p1.x > ed - st) ed = p1.y, st = p1.x; if (p2.y - p2.x > ed - st) ed = p2.y, st = p2.x; } return ed - st + 1; } };