Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**------------------------
- Author : NikolaP
- Compiler : C++
- -------------------------*/
- //{ Setup
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long int ll;
- typedef long double ld;
- typedef vector<int> vi;
- const int inf = INT_MAX;
- const bool debug = 0;
- //}
- struct KMP{
- string text, pattern;
- vi key;
- void process(){
- int len = pattern.size();
- key.assign(len+1,0);
- int i = 0, j = -1;
- key[0] = -1;
- while(i < len){
- while(j >= 0 and pattern[i] != pattern[j]) j = key[j];
- i++; j++;
- key[i] = j;
- }
- }
- KMP(string _text, string _pattern){
- text = _text;
- pattern = _pattern;
- process();
- }
- void Solution(){
- int i = 0, j = 0;
- while(i < text.size()){
- while(j >= 0 and text[i] != pattern[j]) j = key[j];
- i++; j++;
- if(j == pattern.size()){
- cout << "Index: " << i-j << '\n';
- j = key[j];
- }
- }
- }
- };
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.precision(8);
- //cout << fixed;
- string a,b;
- getline(cin,a);
- getline(cin,b);
- KMP kmp = KMP(a,b);
- kmp.Solution();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment