QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#504924 | #9111. Zayin and String | PhantomThreshold# | WA | 57ms | 46536kb | C++20 | 2.5kb | 2024-08-04 17:18:16 | 2024-08-04 17:18:17 |
Judging History
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[maxp][26],sum[maxp],fail[maxp];
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
Wrong Answer
time: 57ms
memory: 46536kb
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:
92946 32219 0 96670 33418 0 61572 67112 69859 0 0 0 89474 38534 42618 68480 0 0 58076 28904 0 52838 14442 0 95383 0 71532 42965 0 0 9419 83640 0 0 83443 93595 0 0 62015 0 59111 99835 88812 920 82142 89457 0 0 36781 89493 84876 0 0 19044 0 0 8635 83585 34864 0 3507 58403 49824 0 0 45951 48695 59259 1...
result:
wrong answer 1st lines differ - expected: '332874949', found: '92946'