Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5.  
  6. void PrintEntries(const std::string& haystack, const std::string& needle) {
  7.  
  8.     const int haystack_size = haystack.size();
  9.     const int needle_size = needle.size();
  10.  
  11.     std::vector<int> z(needle_size, 0);
  12.  
  13.     for (int i = 1; i < needle_size; ++i) {
  14.         int shift = 0;
  15.         while (i + shift < needle_size && needle[shift + i] == needle[shift]) {
  16.             ++shift;
  17.         }
  18.         z[i] = shift;
  19.     }
  20.  
  21.     int left = 0;
  22.     int right = 0;
  23.  
  24.     for (int i = 0; i < haystack_size; ++i) {
  25.         int shift = (i <= right) ? std::min(z[i - left], right - i + 1) : 0;
  26.         while (shift + i < haystack_size && shift < needle_size
  27.                && (haystack[i + shift] == needle[shift])) {
  28.             ++shift;
  29.         }
  30.         if (shift == needle_size) {
  31.             std::cout << i << " ";
  32.         }
  33.         if (shift + i - 1 > right) {
  34.             left = i;
  35.             right = shift + i - 1;
  36.         }
  37.     }
  38.     std::cout << std::endl;
  39. }
  40. int main() {
  41.     std::string needle, haystack;
  42.     std::cin >> needle >> haystack;
  43.  
  44.     PrintEntries(haystack, needle);
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement