QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#504921#9111. Zayin and StringPhantomThreshold#RE 0ms0kbC++202.5kb2024-08-04 17:16:402024-08-04 17:16:40

Judging History

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

  • [2024-08-04 17:16:40]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-08-04 17:16:40]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn = 1010;
const int maxp = 4010;
const int mod  = 998244353;
const double eps = 1e-9;

int pw(int x,int k)
{
	int re=1;
	for(;k;k>>=1,x=(ll)x*x%mod) if(k&1)
		re=(ll)re*x%mod;
	return re;
}
int inv(int x){ return pw(x,mod-2); }

struct Trie
{
	int rt,tot;
	int son[maxn][26],sum[maxn],fail[maxn];
	int newnode()
	{
		++tot;
		memset(son[tot],0,sizeof son[tot]);
		sum[tot]=fail[tot]=0;
		return tot;
	}
	void init()
	{
		rt=tot=0;
		rt=newnode();
	}
	void insert(string ss,const int val)
	{
		int x=rt;
		for(auto c:ss)
		{
			int w=c-'a';
			if(!son[x][w]) son[x][w]=newnode();
			x=son[x][w];
		}
		sum[x]+=val;
	}
	void build()
	{
		queue<int>q;
		q.push(rt); fail[rt]=0;
		while(!q.empty())
		{
			const int x=q.front(); q.pop();
			int fl=fail[x];
			sum[x]+=sum[fl];
			for(int w=0;w<26;w++)
			{
				int &y=son[x][w];
				if(y)
				{
					if(x!=rt) fail[y]=son[fl][w];
					else fail[y]=x;
				}
				else
				{
					if(x!=rt) y=son[fl][w];
					else y=rt;
				}
			}
		}
	}
}tr;

int n,m;
string S;

int tf[maxn][maxp],tn;
double f[maxn][maxp];
int pf[maxn][maxp];
inline void upd(const int i,const int j,const int F,const int pj)
{
	if(tf[i][j]!=tn) 
	{
		tf[i][j]=tn, f[i][j]=F;
		pf[i][j]=pj;
	}
	else if(f[i][j]>F) f[i][j]=F, pf[i][j]=pj;
}
int check(const double mid)
{
	f[0][tr.rt]=0;
	tf[0][tr.rt]=tn;
	for(int i=0;i<=n;i++)
	{
		for(int j=1;j<=tr.tot;j++) if(tf[i][j]==tn)
		{
			if(i && f[i][j]>eps) return 1;
			if(i<n)
			{
				upd(i+1,j,f[i][j],-j);
				int w=S[i+1]-'a';
				int nj= tr.son[j][w];
				upd(i+1,nj,f[i][j]-mid+tr.sum[nj],j);
			}
		}
	}
	return 0;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int Tcase; cin>>Tcase;
	while(Tcase--)
	{
		cin>>n>>m;
		cin>>S; S=' '+S;
		
		tr.init();
		double l=0,r=0;
		for(int i=1;i<=m;i++)
		{
			string ss; cin>>ss;
			int c; cin>>c;
			tr.insert(ss,c);
			r+=c;
		}
		
		tn=0;
		for(int ti=1;ti<=50;ti++)
		{
			double mid=(l+r)/2;
			++tn;
			if(check(mid)) l=mid;
			else r=mid;
		}
		++tn; check(l);
		
		int edi=0,edj;
		for(int i=1;i<=n && edi==0;i++) for(int j=1;j<=tr.tot;j++) if(tf[i][j]==tn)
		{
			if(f[i][j]>eps)
			{
				edi=i,edj=j;
				break;
			}
		}
		ll ansp=0,ansq=0;
		while(edi)
		{
			ansp+=tr.sum[edj];
			ansq+=(pf[edi][edj]>0);
			edj=abs( pf[edi][edj] );
			edi--;
		}
		cout<<ansp%mod*inv(ansq)%mod<<'\n';
	}
	
	return 0;
}

详细

Test #1:

score: 0
Runtime Error

input:

80
4 7
ggeg
g 92946
d 65678
gg 50828
wrj 93954
gge 53780
a 94179
geg 40837
5 6
hiiii
ii 67984
foh 69185
hi 88153
pj 39431
i 32219
wfmve 96834
8 12
wvxxvwww
xw 1522
rzl 16262
wx 77596
v 69622
vw 82225
nykkmkv 19236
xv 83470
o 16781
w 2405
m 98319
ww 13889
qggbvty 16331
8 14
jjjiiijh
i 96670
z 74397
i...

output:


result: