Guest User

Untitled

a guest
Jan 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. char text[1000002];
  7. int pre_suf[1000002];
  8.  
  9. void clear(int n)
  10. {
  11.     for(int k=0; k<=n; k++)
  12.         pre_suf[k]=0;
  13. }
  14.  
  15. int main()
  16. {
  17.     int z;
  18.     string input;
  19.    
  20.     cin >> z;
  21.     for(int x=1; x<=z; x++)
  22.     {
  23.         cin >> input;
  24.        
  25.         int d=input.length();
  26.         clear(d);
  27.         for(int i=0; i<d; i++)
  28.             text[i+1]=input[i];
  29.        
  30.         int counter=0;
  31.         for(int i=2; i<=d; i++)
  32.         {
  33.             while(counter>0 && text[i]!=text[counter+1])
  34.                 counter=pre_suf[counter];
  35.             if(text[i]==text[counter+1])
  36.                 counter++;
  37.             pre_suf[i]=counter;
  38.         }
  39.        
  40.         for(int i=0; i<=d; i++)
  41.             cout << pre_suf[i] << " ";
  42.         cout << endl;
  43.     }
  44.    
  45.     system("pause");
  46.     return 0;
  47. }
Add Comment
Please, Sign In to add comment