求助大佬,也是RE问题,数组开的够大,自己遍的数据也能过
查看原帖
求助大佬,也是RE问题,数组开的够大,自己遍的数据也能过
246441
jfy18205770651楼主2020/7/22 20:50
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<list>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
int match[maxn*2];
void get_match(string s2){
	match[0]=-1;
	int len=s2.length();
	for(int j=1;j<len;j++){
		int temp=match[j-1];
		while(temp>=0&&s2[temp+1]!=s2[j]){
			temp=match[temp];
		}
		if(s2[temp+1]==s2[j]) match[j]=temp+1;
		else match[j]=-1;
	}
}
int KMP(string s1,string s2){
	get_match(s2);
	int s=0,p=0;
	while(s<s1.length()){
		if(s1[s]==s2[p]){
			s++;
			p++;
		}
		else if(p>0) p=match[p-1]+1;
		else s++;
		if(p==s2.length()) cout<<s-p+1<<'\n',p=1;
	}
}
int main()
{
	string s1,s2;
	getline(cin,s1);
	getline(cin,s2);
	KMP(s1,s2);
	for(int i=0;i<s2.length();i++){
		cout<<match[i]+1<<' ';
	}
	return 0;
 } 
2020/7/22 20:50
加载中...