Advertisement
smatskevich

Solutions7

Jan 13th, 2022
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7. typedef long long ll;
  8.  
  9. int main() {
  10.   string s;
  11.   cin >> s;
  12.   int n = s.size();
  13.  
  14.   vector<int> d1 (n);
  15.   int l=0, r=-1;
  16.   for (int i=0; i<n; ++i) {
  17.     int k = i>r ? 1 : min(d1[l+r-i], r-i+1);
  18.     while (i+k < n && i-k >= 0 && s[i+k] == s[i-k])  ++k;
  19.     d1[i] = k;
  20.     if (i+k-1 > r) {
  21.       l = i - k + 1;
  22.       r = i + k - 1;
  23.     }
  24.   }
  25.   vector<int> d2 (n);
  26.   l=0, r=-1;
  27.   for (int i=0; i<n; ++i) {
  28.     int k = i>r ? 0 : min(d2[l+r-i+1], r-i+1);
  29.     while (i+k < n && i-k-1 >= 0 && s[i+k] == s[i-k-1])  ++k;
  30.     d2[i] = k;
  31.     if (i+k-1 > r) {
  32.       l = i - k;
  33.       r = i + k - 1;
  34.     }
  35.   }
  36.  
  37.   ll result = 0;
  38.   for (int x : d1) result += x - 1;
  39.   for (int x : d2) result += x;
  40.  
  41.   cout << result << endl;
  42.  
  43.   return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement