StoneHaos

12

Nov 29th, 2021 (edited)
96
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int BM(string s, string t) {
  7.     int n = s.size();
  8.     int m = t.size();
  9.     vector<int> v;
  10.     for (int i = 0; i <= n - m; ++ i) {
  11.         if (s[i] == t[0])
  12.             v.push_back(i);
  13.     }
  14.     for (int i = 0; i < v.size(); ++ i) {
  15.         int j = m - 1;
  16.         while (j >= 0 && s[v[i] + j] == t[j]) j--;
  17.         if (j == -1) return v[i];
  18.     }
  19.     return -1;
  20. }
  21.  
  22. int main() {
  23.     string s, t;
  24.     cin >> s >> t;
  25.     cout << BM(s, t) << endl;
  26.     return 0;
  27. }
RAW Paste Data Copied