Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define maxm 10001
- using namespace std;
- int L, R, k;
- string str, patt, S;
- void calculateZ(){
- vector<int> Z(S.size());
- L = R = 0;
- for(int i=1; i<S.size(); ++i){
- if(i > R){
- L = R = i;
- while(R < S.size() && S[i] == S[R-L]){
- R++;
- }
- Z[i] = R - L;
- R--;
- }
- else{
- k = i - L;
- if(Z[k] < R - i + 1){
- Z[i] = Z[k];
- }
- else{
- while(R < S.size() && S[i] == S[R-L]){
- R++;
- }
- Z[i] = R - L;
- R--;
- }
- }
- }
- cout<<"Z = ";
- for(int i=0; i<S.size(); ++i)
- cout<<Z[i]<<" ";
- cout<<"\nPositions in string:\n";
- for(int i=patt.size()+1; i<S.size(); ++i)
- cout<<Z[i]<<" ";
- cout<<"\n";
- }
- int main(){
- S="";
- cin>>str>>patt;
- S += patt + "$" + str;
- calculateZ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement