Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <map>
- #include <windows.h>
- using namespace std;
- int bauermooresearch(string& s, string& p) {
- int m = p.length();
- map <char, int> t;
- for (int i = 0; i < m - 1; ++i)
- t[p[i]] = m - 1 - i;
- int i = m - 1, j = m - 1;
- int n = s.length();
- while (i < n)
- if (p[j] == s[i]) {
- if (j == 0)
- return i;
- --j;
- --i;
- } else {
- if (t.find(s[i]) != t.end())
- i += t[s[i]];
- else
- i += m;
- j = m - 1;
- }
- return -1;
- }
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- string p, s;
- cout << "Текст: ";
- getline(cin, s);
- cout << "Строка: ";
- getline(cin, p);
- int ans = bauermooresearch(s, p);
- if (ans == -1)
- cout << "Строка в тексте не найдена!" << endl;
- else
- cout << "Позиция первого вхождения строки в текст: " << ans << "." << endl;
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment