Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using ull = unsigned long long;
- const unsigned K = 31;
- const ull P = (1ull << K) - 1;
- inline ull mod(ll x){
- if(x < P)
- return x;
- x = (x >> K) + (x & P);
- x = (x >> K) + (x & P);
- return x == P ? 0 : x;
- }
- struct H{
- unsigned h[4000001], p[4000001];
- H(int size, char* s){
- p[0] = 1;
- for(size_t i = 0; i < size; i++){
- p[i + 1] = mod((ll)p[i] * K);
- h[i + 1] = mod((ll)h[i] * K + s[i] - 'a' + 1);
- }
- }
- inline ull get(int id, int len){
- return mod(h[id + len - 1] - (ll)h[id - 1] * p[len]);
- }
- };
- int n;
- char s[4000001];
- H* h;
- H* rh;
- inline bool equal(int len, int beg){
- return h->get(1, len) == rh->get(n - beg + 1, len);
- }
- int binSearch(int x){
- int begin = 0, end = x + 1, middle;
- while(end - begin > 1){
- middle = (begin + end) / 2;
- if(equal(middle, x))
- begin = middle;
- else
- end = middle;
- }
- return begin;
- }
- int main(){
- scanf("%d", &n);
- do{
- s[0] = getchar_unlocked();
- }while(s[0] < 'a' or s[0] > 'z');
- for(int i = 1; i < n; i++)
- s[i] = getchar_unlocked();
- h = new H(n, s);
- for(int i = 0; i < n / 2; i++)
- swap(s[i], s[n - i - 1]);
- rh = new H(n, s);
- for(int i = 1; i <= n; i++)
- printf("%d ", binSearch(i));
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement