loulett

manaker

Dec 7th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <vector>
  2. #include <array>
  3. #include <iostream>
  4. #include <string>
  5. #include <algorithm>
  6. #include <utility>
  7.  
  8. const int MAXN = 500000;
  9. std::array<char, MAXN> s;
  10. std::array<int, MAXN> rad;
  11.  
  12. int main() {
  13.     std::string str;
  14.     std::cin >> str;
  15.     int n = str.size();
  16.     for (int i = 0; i < n; i++)
  17.         s[i] = str[i];
  18.  
  19.     int ansL = 0;
  20.     int ansR = 0;
  21.  
  22.     for (int z = 0; z < 2; ++z) {
  23.         int l = 0, r = -1;
  24.         for (int i = 0; i < n; ++i) {
  25.             if (i > r) {
  26.                 rad[i] = 1 - z;
  27.             }
  28.             else {
  29.                 rad[i] = std::min(rad[l + r - i + z], r - i + z);
  30.             }
  31.             while (rad[i] + z <= i && i + rad[i] < n && s[i + rad[i]] == s[i - rad[i] - z]) {
  32.                 ++rad[i];
  33.             }
  34.             if (i + rad[i] - z > r) {
  35.                 l = i - rad[i] + (1 - z);
  36.                 r = i + rad[i] - 1;
  37.             }
  38.             if (ansR - ansL < i + rad[i] - (i - rad[i] - z) - 1) {
  39.                 ansL = i - rad[i] - z + 1;
  40.                 ansR = i + rad[i];
  41.             }
  42.         }
  43.     }
  44.  
  45.     for (int i = ansL; i < ansR; i++)
  46.         printf("%c", s[i]);
  47.     return 0;
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment