Advertisement
istinishat

Better_Pi_Function

May 24th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #define MAX 100005
  5.  
  6. int pi[MAX];
  7.  
  8. void pi_fun(string str)
  9. {
  10.     for(int i=1;i<str.size();i++){
  11.         int j=pi[i-1];
  12.         while(j>0 && str[i] != str[j])
  13.             j=pi[j-1];
  14.         if(str[i]==str[j])
  15.             j++;
  16.         pi[i]=j;
  17.     }
  18. }
  19.  
  20. int main()
  21. {
  22.     int i;
  23.     string str;
  24.     cin>>str;
  25.     pi_fun(str);
  26.     for(i=0;i<str.size();i++)
  27.         cout<<pi[i]<<' ';
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement