fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int expandAroundCenter(string s, int left, int right)
  5. {
  6. int n = s.length();
  7. while (left >= 0 && right < n && s[left] == s[right])
  8. {
  9. left--;
  10. right++;
  11. }
  12. return right - left - 1;
  13. }
  14.  
  15. int longestPalindrome(string s)
  16. {
  17. if (s.empty()) return 0;
  18. int maxLen = 1;
  19. for (int i = 0; i < s.length(); i++)
  20. {
  21. int len1 = expandAroundCenter(s, i, i);
  22. int len2 = expandAroundCenter(s, i, i + 1);
  23. maxLen = max(maxLen, max(len1, len2));
  24. }
  25. return maxLen;
  26. }
  27.  
  28. int main()
  29. {
  30. string s;
  31. cin >> s;
  32. cout << longestPalindrome(s);
  33. return 0;
  34. }
  35. //form bíp
  36.  
Success #stdin #stdout 0s 5276KB
stdin
babad
stdout
3