QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#598278#7876. Cyclic SubstringsHirroTL 0ms0kbC++203.2kb2024-09-28 21:08:242024-09-28 21:08:24

Judging History

你现在查看的是最新测评结果

  • [2024-09-28 21:08:24]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-09-28 21:08:24]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=3e6+10;
const int mod=998244353;
int i,j,n,m,k,l,a[N*4],tree[N*4][11],root,num,r,d[N*4],wz,f[N*4][24],pos[N*4],g[N*2],qz[30],x,y,lg[N*2+1];
long long ans;
string s;
inline void update(int x)
{
	for(int i=1;i<=23;++i)
		f[x][i]=f[f[x][i-1]][i-1];
}
inline int up(int x,int y)
{
	if(!y) return x;
	for(int i=lg[y];i>=0;i=lg[y]) if(qz[i]<=y) x=f[x][i],y-=qz[i];
	return x;
}
inline void dfs1(int x,int y,int dep,int hh)
{
	for(int i=0;i<=10;++i)
		if(tree[x][i])
		{
			if(x==0)
			{
				if(i==10) dfs1(tree[x][i],1,dep+1,i);
				else dfs1(tree[x][i],0,dep+1,i);
			 } 
			else dfs1(tree[x][i],y,dep+1,i); 
			g[x]+=g[tree[x][i]];g[x]%=mod;
		}
	if(x&&hh!=10)
	{
		if(y)
		{
			int gs=dep/2; gs*=2;
			if(gs<=n)ans+=1ll*gs*g[x]%mod*g[x]%mod;
			ans%=mod;
		}
		else
		{
			int gs=dep/2+(dep%2!=0); gs=gs*2-1;
			if(gs<=n)ans+=1ll*gs*g[x]%mod*g[x]%mod;
			ans%=mod;
		}
	}
}
int main()
{
	//freopen("1.in","r",stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	for (int i = 1; i <= N*2; i++)lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
	cin>>n>>s;
	a[0]=0;a[++a[0]]=10;
	for(i=1;i<=n;++i)
	{
		a[++a[0]]=s[i-1]-'0';
		a[++a[0]]=10;
	}
	for(i=1;i<=n;++i)
	{
		a[++a[0]]=s[i-1]-'0';
		a[++a[0]]=10;
	}
	qz[0]=1;
	for(i=1;i<=23;++i) qz[i]=qz[i-1]*2;
	root=0;num=0;
	for(i=1,l=1,r=0;i<=2*n;++i)
	{
		wz=root;
		if(!tree[wz][a[i]]){
			tree[wz][a[i]]=++num;
			f[num][0]=wz;
			update(num);
		}
		wz=tree[root][a[i]];
		if(i>r)
		{
			k=1;
			g[wz]++;
			g[f[wz][0]]--;
		}
		else
		{
			if(d[l+r-i]<=r-i+1)
			{
				k=d[l+r-i];
				x=pos[l+r-i];
				g[x]++;
				g[f[wz][0]]--;
				wz=x;
			}
			else
			{
				k=r-i+1;
				y=d[l+r-i]-(r-i+1);
				x=pos[l+r-i];
				x=up(x,y);
				g[x]++;
				g[f[wz][0]]--;
				wz=x;
			}	
		}
		
		while(1<=i-k&&i+k<=4*n+2&&a[i-k]==a[i+k]){
			if(!tree[wz][a[i+k]])
			{
				tree[wz][a[i+k]]=++num;
				f[num][0]=wz;
				update(num);
			}
			g[wz]--;
			g[tree[wz][a[i+k]]]++;
			wz=tree[wz][a[i+k]];
			k++;
		}
		pos[i]=wz;
		d[i]=k--;
		if(i+k>r){
			l=i-k;
			r=i+k;
		}
	}
	for(i=2*n+1,l=1,r=0;i<=4*n+1;++i)
	{
		wz=root;
		if(!tree[wz][a[i]]){
			tree[wz][a[i]]=++num;
			f[num][0]=wz;
			update(num);
		}
		wz=tree[root][a[i]];
		if(i>r)
		{
			k=1;
			g[wz]++;
			g[f[wz][0]]--;
		}
		else
		{
			if(d[l+r-i]<=r-i+1)
			{
				k=d[l+r-i];
				x=pos[l+r-i];
				g[x]++;
				g[f[wz][0]]--;
				wz=x;
			}
			else
			{
				k=r-i+1;
				y=d[l+r-i]-(r-i+1);
				x=pos[l+r-i];
				x=up(x,y);
				g[x]++;
				g[f[wz][0]]--;
				wz=x;
			}	
		}
		
		while(1<=i-k&&i+k<=4*n+2&&a[i-k]==a[i+k]){
			if(!tree[wz][a[i+k]])
			{
				tree[wz][a[i+k]]=++num;
				f[num][0]=wz;
				update(num);
			}
			g[wz]--;
			g[tree[wz][a[i+k]]]++;
			wz=tree[wz][a[i+k]];
			k++;
		}
		pos[i]=wz;
		d[i]=k--;
		int len=0;
		if(i-d[i]+1<=2*n) len=i-(2*n);
		else len=d[i];
		x=pos[i]; y=d[i]-len;
		x=up(x,y);
		g[x]--;
		if(i+k>r){
			l=i-k;
			r=i+k;
		}
	}
	dfs1(root,0,0,10);
	ans=(ans%mod+mod)%mod;
	cout<<ans<<endl;
}

詳細信息

Test #1:

score: 0
Time Limit Exceeded

input:

5
01010

output:


result: