The KMP algorithm is an algorithm that can match two strings in O(n+m) time complexity.
AcWing 831. KMP string
Given a pattern string S and a template string P, all strings only contain uppercase and lowercase English letters and Arabic numerals. The template string P appears as a substring multiple times in the pattern string S.
Calculate the initial index of all the positions of the template string P in the pattern string S.
Input format
Input the integer N in the first line, which represents the length of the string P.
Enter the string P on the second line.
Enter the integer M in the third line, which represents the length of the string S.
Enter the string S on the fourth line.
Output format
One line, output the starting subscripts of all occurrence positions (subscripts start counting from 0), and the integers are separated by spaces.
(1≤N≤1e5,1≤M≤1e6)
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define endl '\n'
#include<algorithm>
using namespace std;
inline ll read()
{
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1; ch=getchar(); }
while('0'<=ch&&ch<='9') x=x*10+ch-'0', ch=getchar();
return f*x;
}
const int maxn = 1e6+5;
int n,m,ne[maxn];
char s[maxn],p[maxn];
int main()
{
cin>>n; cin>>p+1; //从1开始存储字符串,也可以从0开始,不过会有一些小变化,下面会有从0开始的代码
cin>>m; cin>>s+1;
//求next数组(此处不是很懂 (* ̄︿ ̄) )
for(int i=2,j=0;i<=n;i++)
{
while(j&&p[i]!=p[j+1]) j=ne[j];
if(p[i]==p[j+1]) j++;
ne[i]=j;
}
// KMP匹配
for(int i=1,j=0;i<=m;i++)
{
while(j&&s[i]!=p[j+1]) j=ne[j];//如果p[j]后面的字符与s[i]不匹配,字符串p就要向后移动
if(s[i]==p[j+1]) j++;//匹配后,j++,继续下一个
if(j==n)//j == n说明匹配成功
{
cout<<i-n<<" ";
j=ne[j];//因为有可能多次匹配,所以匹配还要继续
}
}
return 0;
}
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define endl '\n'
#include<algorithm>
using namespace std;
inline ll read()
{
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1; ch=getchar(); }
while('0'<=ch&&ch<='9') x=x*10+ch-'0', ch=getchar();
return f*x;
}
const int maxn = 1e6+5;
int n,m,ne[maxn];
string s,p;
int main()
{
cin>>n; cin>>p; //下标从0开始
cin>>m; cin>>s;
ne[0]=-1; //0处的next指针指向-1,表示空
//求next数组
for(int i=1,j=-1;i<n;i++)
{
while(~j&&p[i]!=p[j+1]) j=ne[j];//判断条件为 j!=-1 (即~j)
if(p[i]==p[j+1]) j++;
ne[i]=j;
}
// KMP匹配
for(int i=0,j=-1;i<m;i++)
{
while(~j&&s[i]!=p[j+1]) j=ne[j];
if(s[i]==p[j+1]) j++;
if(j==n-1)
{
cout<<i-n+1<<" ";
j=ne[j];
}
}
return 0;
}