Advertisement
smatskevich

SubstringSearch

Jan 13th, 2022
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7. typedef long long ll;
  8.  
  9. int main() {
  10.   string p, t;
  11.   cin >> p >> t;
  12.  
  13.   vector<int> pi(p.size());
  14.   int j = 0;
  15.   for (size_t i = 1; i < p.size(); i++) {
  16.     while (j > 0 && p[j] != p[i]) j = pi[j - 1];
  17.     if (p[j] == p[i]) pi[i] = ++j;
  18.   }
  19.  
  20.   j = 0;
  21.   for (size_t i = 0; i < t.size(); i++) {
  22.     while (j > 0 && p[j] != t[i]) j = pi[j - 1];
  23.     if (p[j] == t[i]) ++j;
  24.     if (j == p.size()) cout << i + 1 - p.size() << " ";
  25.   }
  26.  
  27.   cout << endl;
  28.   return 0;
  29. }
  30.  
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement