Advertisement
shek_shek

201

Dec 18th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stack>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <string>
  8. #include <set>
  9. #include <iomanip>
  10. #include <vector>
  11. #include <map>
  12. #include <cassert>
  13. #include <queue>
  14. #include <tuple>
  15.  
  16. using namespace std;
  17.  
  18. typedef long long li;
  19. typedef long double ld;
  20.  
  21. #define forn(i, n) for (int i = 0; i < int(n); ++i)
  22. #define pb push_back
  23. #define mp make_pair
  24. #define shek_shek _DEBUG
  25. #define all(v) v.begin(),v.end()
  26. #define EPS 1e-9
  27. #define PI 3.1415926535897932384626433832795
  28. const int N = 111111;
  29.  
  30. const li A = 31;
  31. const li MOD = 99991;
  32.  
  33. int main () {
  34. #ifdef shek_shek
  35.     freopen("input.txt", " r", stdin);
  36.     freopen("output.txt", "w", stdout);
  37. #endif
  38.     string p, t;
  39.     cin >> p >> t;
  40.  
  41.     li p_hash = 0, t_hash = 0, p_pow = 1;
  42.  
  43.     if (p.size() > t.size())
  44.         return 0;
  45.  
  46.     for (int i = 0; i < p.size(); i++)  {
  47.         p_hash = p_hash * A + (p[i] - 'a' + 1);
  48.         t_hash = t_hash * A + (t[i] - 'a' + 1);
  49.         if (i > 0)
  50.             p_pow *= A;
  51.         //p_hash %= MOD;
  52.         //t_hash %= MOD;
  53.     }
  54.  
  55.     vector <int> ans;
  56.  
  57.     forn (i, t.size() - p.size() + 1) {
  58.         if (p_hash == t_hash)
  59.             ans.push_back(i + 1);
  60.         t_hash -= p_pow * (t[i] - 'a' + 1);
  61.         t_hash = t_hash * A + (t[i + p.size()] - 'a' + 1);     
  62.         //t_hash %= MOD;
  63.     }
  64.  
  65.     forn(i, ans.size())
  66.         printf("%d ", ans[i]);
  67.  
  68.  
  69.  
  70.     return 0;
  71. }
  72. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement