fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int hashing(int p, string st, int start, int end) {
  5. long long hash = 0;
  6. for (int i = start; i <= end; i++)
  7. hash = (hash * 32 + (st[i] - 'a' + 1)) % p;
  8. return hash;
  9. }
  10.  
  11. //151527006
  12. //tpj
  13. //jnm
  14.  
  15. //354924896
  16. //cwugbhvaqobsbohekgyvrqjcjipcuwu
  17. //sbo
  18.  
  19. int main() {
  20. ios::sync_with_stdio(false);
  21. cin.tie(nullptr);
  22. cout.tie(nullptr);
  23. int p;
  24. string s, t;
  25. cin >> p;
  26. cin >> s;
  27. cin >> t;
  28.  
  29. int h2 = hashing(p, t, 0, t.size() - 1);
  30. bool flag = false;
  31. for (int i = 0; i <= s.size() - t.size(); i++) {
  32. if (s[i] == t[0] && s[i + t.size() - 1] == t[t.size() - 1]) {
  33. int h1 = hashing(p, s, i, i + t.size() - 1);
  34. if (h1 == h2) {
  35. cout << i;
  36. flag = true;
  37. break;
  38. }
  39. }
  40. }
  41. if (!flag)
  42. cout << -1;
  43. }
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
Standard output is empty