#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string l;
string s;
cin >> l;
cin >> s;
vector<long long>v(s.size(),0);
int i, j;
i = 0;
j = 0;
for (int i = 0; i < s.size(); i++) {
i++;
if (s[j] == s[i]) {
v[i] = v[i - 1] + 1;
j++;
}
else {
if (j == 0) {
j = v[j ];
}
else {
j = v[j - 1];
}
}
}
j = 0;
for (int i = 0; i < l.size(); i++) {
if (l[i] == s[j]) {
j++;
if (j = s.size()) {
cout << "start= " << i - s.size() << " end= " << i ;
i = l.size();
}
}
else {
if (j == 0) {
j = 0;
}
else {
j = v[j - 1];
}
}
}
}
#define ll long long int
#define vll vector<long long int>
ll kmp(string s)
{
ll n = s.size();
vll lps(n, 0); // longest prefix suffix
for (ll i = 1; i < n; i++)
{
ll j = lps[i - 1];
while (j > 0 && s[i] != s[j])
{
j = lps[j - 1];
}
if (s[i] == s[j])
{
j++;
}
lps[i] = j;
}
return lps[n - 1];
}
"""
This implementation demonstrates how
to efficiently determine if a pattern
string is a substring of some bigger,
target string.
Example:
For following input values:
substring = "aefcd"
string = "dcaefcdcdaed"
Output would be: True
Let m be the size of the pattern and
n be the size of the target string.
Time complexity: O(n+m)
Space complexity: O(m)
"""
def kmp_algorithm(string, substring):
i, j = 0, 0
"""
preprocess the pattern string by computing
a failure function mapping indexes to shifts
with the aim of reusing previously performed
comparisons.
"""
failure = compute_failure_function(substring)
str_len, substr_len = len(string), len(substring)
while i < str_len:
if string[i] == substring[j]:
# Pattern is found when its last char reached
if j == substr_len - 1:
return True
i += 1
j += 1
elif j > 0:
# Determine next continuation index in pattern
# by consulting the failure function.
j = failure[j-1]
else:
i += 1
return False
def compute_failure_function(substring):
# The failure function maps each index j
# in pattern P to length of longest prefix
# of P that is a suffix of P[1:j]
j, i = 0, 1
substr_len = len(substring)
failure = [0] * substr_len
while i < substr_len:
if substring[j] == substring[i]:
# We have matched j + 1 characters
failure[i] = j + 1
i += 1
j += 1
elif j > 0:
# Place j just after a prefix that matches
j = failure[j-1]
else:
i += 1
return failure
print(kmp_algorithm("dcaefcdcdaed", "aefcd")) # True
print(kmp_algorithm("dcaefccdaed", "aefcd")) # False
COMPUTE- PREFIX- FUNCTION (P)
1. m ←length [P] //'p' pattern to be matched
2. Π [1] ← 0
3. k ← 0
4. for q ← 2 to m
5. do while k > 0 and P [k + 1] ≠ P [q]
6. do k ← Π [k]
7. If P [k + 1] = P [q]
8. then k← k + 1
9. Π [q] ← k
10. Return Π
KMP-MATCHER (T, P)
1. n ← length [T]
2. m ← length [P]
3. Π← COMPUTE-PREFIX-FUNCTION (P)
4. q ← 0 // numbers of characters matched
5. for i ← 1 to n // scan S from left to right
6. do while q > 0 and P [q + 1] ≠ T [i]
7. do q ← Π [q] // next character does not match
8. If P [q + 1] = T [i]
9. then q ← q + 1 // next character matches
10. If q = m // is all of p matched?
11. then print "Pattern occurs with shift" i - m
12. q ← Π [q] // look for the next match