这种思路不对吗
查看原帖
这种思路不对吗
936616
zibenlun楼主2023/9/7 20:29
#include<bits/stdc++.h>
using namespace std;
inline long long read()
{
	long long s=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') {
		s=(s<<3)+(s<<1)+(ch^48);
		ch=getchar();
	}
	return s;
}
inline void write(long long x)
{
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
int a[1000005],sum[1000005],n,lg[1000005]={1};
const long long mod = 998244353;
bool check(int x){
	map<long long,bool> s;
	for(int i=x;i<=n;i++){
		long long cnt=(sum[i]-sum[i-x]*lg[x] + mod ) %mod;
		if(s[cnt]==0) s[cnt]=1;
		else return 1;
	}
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	string s;
	cin>>s;
	n=s.size();
	for(int i=1;i<=n;i++) {
		lg[i]=lg[i-1]*29%mod;
	}
	for(int i=0;i<s.size();i++)
	{
		a[i+1]=s[i]-'a'+1;
		sum[i+1]=((sum[i]*29)+a[i+1])%mod;
	}
	int l=0,r=n,ans=0;
	while(l<r)
	{
		int mid=(l+r)/2;
		if(check(mid)){
			ans=max(ans,mid);
			l=mid+1;
		}
		else {
			r=mid;
		}
	}
	cout<<ans;
	return 0;
}

能过样例,但是评测全错。

2023/9/7 20:29
加载中...