Advertisement
anoosykh95

bpm

Jul 21st, 2016
2,737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void getZarr(string str, int Z[]);
  5.  
  6. //  prints all occurrences of pattern in text using Z algo
  7. void search(string text, string pattern)
  8. {
  9.     // Create concatenated string "P$T"
  10.     string concat = pattern + "$" + text;
  11.     int l = concat.length();
  12.  
  13.     // Construct Z array
  14.     int Z[l];
  15.     getZarr(concat, Z);
  16.  
  17.     //  now looping through Z array for matching condition
  18.     for (int i = 0; i < l; ++i)
  19.    {
  20.        // if Z[i] (matched region) is equal to pattern
  21.        // length  we got the pattern
  22.        if (Z[i] == pattern.length())
  23.            cout << "Pattern found at index "
  24.                 <<  i - pattern.length() -1 << endl;
  25.    }
  26. }
  27.  
  28. //  Fills Z array for given string str[]
  29. void getZarr(string str, int Z[])
  30. {
  31.    int n = str.length();
  32.    int L, R, k;
  33.  
  34.    // [L,R] make a window which matches with prefix of s
  35.    L = R = 0;
  36.    for (int i = 1; i < n; ++i)
  37.    {
  38.        // if i>R nothing matches so we will calculate.
  39.         // Z[i] using naive way.
  40.         if (i > R)
  41.         {
  42.             L = R = i;
  43.  
  44.             // R-L = 0 in starting, so it will start
  45.             // checking from 0'th index. For example,
  46.             // for "ababab" and i = 1, the value of R
  47.             // remains 0 and Z[i] becomes 0. For string
  48.             // "aaaaaa" and i = 1, Z[i] and R become 5
  49.             while (R<n && str[R-L] == str[R])
  50.                R++;
  51.            Z[i] = R-L;
  52.            R--;
  53.        }
  54.        else
  55.        {
  56.            // k = i-L so k corresponds to number which
  57.            // matches in [L,R] interval.
  58.            k = i-L;
  59.  
  60.            // if Z[k] is less than remaining interval
  61.            // then Z[i] will be equal to Z[k].
  62.            // For example, str = "ababab", i = 3, R = 5
  63.            // and L = 2
  64.            if (Z[k] < R-i+1)
  65.                 Z[i] = Z[k];
  66.  
  67.            // For example str = "aaaaaa" and i = 2, R is 5,
  68.            // L is 0
  69.            else
  70.            {
  71.                //  else start from R  and check manually
  72.                L = i;
  73.                while (R<n && str[R-L] == str[R])
  74.                    R++;
  75.                Z[i] = R-L;
  76.                R--;
  77.            }
  78.        }
  79.    }
  80. }
  81. int main()
  82. {
  83.    string text ;
  84.    string pattern;
  85.    cin>>text>>pattern;
  86.     search(text, pattern);
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement