Semior001

palindrome

Nov 1st, 2016
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define F first
  3. #define S second
  4. #define mp make_pair
  5. #define pb push_back
  6. typedef long long ll;
  7. typedef long double ld;
  8.  
  9. using namespace std;
  10.  
  11. int l,r,n;
  12. int d[1000001];
  13. char s[1000001];
  14.  
  15. int main(){
  16.     // cin >> s;
  17.     scanf("%s", s);
  18.     n = strlen(s);
  19.     l = 0;
  20.     r = 0;
  21.     for(int i = 0; i < n; i++){
  22.         if(i <= r){
  23.             d[i] = min(d[l+r-i],r-i+1);
  24.         }
  25.         while(i - d[i] >= 0 &&
  26.             i + d[i] < n &&
  27.             s[i-d[i]] == s[i+d[i]]){
  28.                 d[i]++;
  29.         }
  30.         if(r < i + d[i] - 1){
  31.             l = i - d[i] + 1;
  32.             r = i + d[i] - 1;
  33.         }
  34.     }
  35.     for(int i = 0; i < n; i++){
  36.         // cout << d[i]*2 - 1 << " ";
  37.         d[i] = d[i]*2-1;
  38.         printf("%d ", d[i]);
  39.     }
  40.     cout << endl;
  41.     return 0;
  42. }
Add Comment
Please, Sign In to add comment