Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     string s;
  9.     cin >> s;
  10.     size_t p  = s.length();
  11.     vector <long long> pref_function(p, 0);
  12.     for (size_t i = 1; i < p; ++i){
  13.         size_t k = pref_function[i - 1];
  14.         while ((k > 0) && (s[i] != s[k])) {
  15.             k = pref_function[k - 1];
  16.         }
  17.         if (s[i] == s[k]) {
  18.             k++;
  19.         }
  20.         pref_function[i] = k;
  21.     }
  22.  
  23.     char ch;
  24.     cin.get(ch);
  25.     long long f = 0;
  26.     size_t i = 0;
  27.     cin.get(ch);
  28.     while (ch != '\n'){
  29.         while ((f > 0) && (s[f] != ch)){
  30.             f = pref_function[f - 1];
  31.         }
  32.         if (s[f] == ch) {
  33.             f++;
  34.         }
  35.         if (f == p) {
  36.             cout << i - p + 1 << endl;
  37.         }
  38.         i++;
  39.         cin.get(ch);
  40.     }
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement