Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define fast_read ios::sync_with_stdio(0);cin.tie(0);
- #define ll long long
- using namespace std;
- void kmp(string a,string b) {
- int n = a.length(),m = b.length(),len = 0,i = 1,j = 0;
- int lps[m];
- lps[0] = 0;
- while (i < m) {
- if (b[i] == b[len]) {
- len++;
- lps[i] = len;
- i++;
- }
- else {
- if (len == 0) {
- lps[i] = 0;
- i++;
- }
- else len = lps[len - 1];
- }
- }
- i = j;
- while (i < n) {
- if (a[i] == b[j]) {
- i++;
- j++;
- }
- if (j == m) {
- cout<<i-j<<" ";
- j = lps[j - 1];
- }
- if (i < n && a[i] != b[j]) {
- if (j == 0)i++;
- else j = lps[j - 1];
- }
- }
- }
- int main() {
- fast_read;
- string a,b;
- cin>>a>>b;
- kmp(a,b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment