新年第一发 exKMP板子求条
查看原帖
新年第一发 exKMP板子求条
901471
five_rice_water楼主2025/1/1 12:22
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e7+5;
string s,t;
int z[N],e[N],r,c;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>s>>t;
	int n = s.size(), m = t.size();
	z[0]=m;
	r = c = 1;
	for(int i = 1; i<m; i++){
		if(r>i){
			z[i]=min(r-i,z[i-c]);
		}
		else{
			z[i]=0;
		}
		while(i+z[i]<m && t[i]==t[i+z[i]]){
			z[i]++;
		}
		if(i+z[i]>r){
			r=i+z[i];
			c=i;
		}
	}
	r = c = 0;
	for(int i = 0; i<n; i++){
		if(r>i){
			e[i]=min(r-i,z[i-c]);
		}
		else{
			e[i]=0;
		}
		while(i+e[i]<n && e[i]<m && s[i+e[i]]==t[e[i]]){
			e[i]++;
		}
		if(i+e[i]>r){
			r=i+e[i];
			c=i;
		}
	}
	int ans = 0;
	for(int i = 0; i<m; i++){
		ans^=((i+1)*(z[i]+1));
	}
	cout<<ans<<endl;
	ans=0;
	for(int i = 0; i<n; i++){
		ans^=((i+1)*(e[i]+1));
	}
	cout<<ans<<endl;
	return 0;
}

如题 全WA和TLE 但是对着左神代码没看出来一点错误…… 而且样例也过了

2025/1/1 12:22
加载中...