p3501不知道为啥TLE,玄关
查看原帖
p3501不知道为啥TLE,玄关
1100149
mountain_climber楼主2024/5/2 20:32
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10,mod1=1e9+7,mod2=998244353;
int n,base=233,fac1[N],fac2[N];
long long ans=0;
string s;
pair<int,int> h1[N],h2[N];
char rev(char x){
	return (x=='1')?'0':'1';
}
pair<int,int> qry1(int l,int r){
	pair<int,int> ret;
	ret.first=(h1[r].first-h1[l-1].first*fac1[r-l+1]%mod1+mod1)%mod1;
	ret.second=(h1[r].second-h1[l-1].second*fac2[r-l+1]%mod2+mod2)%mod2;
}
pair<int,int> qry2(int l,int r){
	pair<int,int> ret;
	ret.first=(h2[r].first-h2[l-1].first*fac1[r-l+1]%mod1+mod1)%mod1;
	ret.second=(h2[r].second-h2[l-1].second*fac2[r-l+1]%mod2+mod2)%mod2;
}
bool check(int pos,int len){
	pair<int,int> p1=qry1(pos-len+1,pos),p2=qry2(n+1-pos-len,n-pos);
	return (p1.first==p2.first)&&(p1.second==p2.second);
}
int solve(int x){
	int ret=0,l=0,r=min(n-x,x);
	while(l<r){
		int mid=(l+r+1)>>1;
		if(check(x,mid)){
			ret=mid;
			l=mid;
		}
		else r=mid-1;
	}
	return ret;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	cin>>n;
	cin>>s;
	h1[0].first=0,h1[0].second=0;
	h2[0].first=0,h2[0].second=0;
	fac1[0]=fac2[0]=1;
	for(int i=1;i<=n;i++) fac1[i]=(fac1[i-1]*base)%mod1,fac2[i]=(fac2[i-1]*base)%mod2;
	for(int i=0;i<n;i++){
		h1[i+1].first=(h1[i].first*base+s[i])%mod1;
		h1[i+1].second=(h1[i].second*base+s[i])%mod2;
	}
	for(int i=1,j=n-1;i<=n,j>=0;i++,j--){
		//cout<<rev(s[j]);
		h2[i].first=(h2[i-1].first*base+rev(s[j]))%mod1;
		h2[i].second=(h2[i-1].second*base+rev(s[j]))%mod2;
	}
	//cout<<endl;
	for(int i=1;i<=n;i++){
		//cout<<solve(i)<<' ';
		ans+=solve(i);
	}
	cout<<ans<<endl;
	return 0;
}

\rt 玄关,感觉TLE可能是二分的问题?但是能过样例。。。

2024/5/2 20:32
加载中...