Advertisement
Manioc

zzz

Feb 15th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAX 100007
  3.  
  4. using namespace std;
  5. int z[MAX], tam;
  6. string txt;
  7. int func(){
  8.     int l = 0, r = 0;
  9.     for(int i = 0; i < tam; i++){
  10.         if(i > r){
  11.             l = r = i;
  12.             while(r < tam && txt[r] == txt[r-l]) r++;
  13.             z[i] = r-l; r--;
  14.         }else{
  15.             int k = i-l;
  16.             if(z[k] < k-i+1) z[i] = z[k];
  17.             else{
  18.                 l = i;
  19.                 while(r < tam && txt[r] == txt[r-l]) r++;
  20.                 z[i] = r-l; r--;
  21.             }
  22.         }
  23.     }
  24.     z[0] = tam;
  25. }
  26.  
  27. int main(){
  28.     ios::sync_with_stdio(false);
  29.     cin.tie(NULL);
  30.     int num; cin >> num;
  31.     while(num--){
  32.         cin >> txt;
  33.         tam = txt.size();
  34.         reverse(txt.begin(), txt.end());
  35.         func();
  36.         int cases; cin >> cases;
  37.         while(cases--){
  38.             int aux; cin >> aux;
  39.             cout << z[tam-aux] << endl;;
  40.         }
  41.     }
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement