QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#557882#9111. Zayin and StringxlwangAC ✓986ms78444kbC++143.5kb2024-09-11 11:50:292024-09-11 11:50:30

Judging History

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

  • [2024-09-11 11:50:30]
  • 评测
  • 测评结果:AC
  • 用时:986ms
  • 内存:78444kb
  • [2024-09-11 11:50:29]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define pb push_back
#define ld long double
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
#define pii pair<int,int>
#define mk make_pair
using namespace std;
inline int read(){
	int x=0;
	bool f=0;
	char c=getchar();
	while(!isdigit(c)) f|=(c=='-'),c=getchar();
	while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int randfind(int l,int r){return rnd()%(r-l+1)+l;}
const int Maxn=1e4+10,mod=998244353;
const ld eps=1e-15;
char s[Maxn],c[Maxn];
int trie[Maxn][26],val[Maxn];
int nod=1;
int fail[Maxn];
int sum[Maxn];
inline int getid(char c){return c-'a';}
inline void clear(){
    fr(i,1,nod) fr(j,0,25) trie[i][j]=0;
    fr(i,1,nod) fail[i]=val[i]=sum[i]=0;
    nod=1;
}
inline void ins(int v){
    int ln=strlen(c+1);
    int now=1;
    fr(i,1,ln){
        if(!trie[now][getid(c[i])]) trie[now][getid(c[i])]=++nod;
        now=trie[now][getid(c[i])];
    }val[now]+=v;
}
int n,q;
inline void make_trie(){
    queue<int> q;
    fr(i,0,25){
        if(!trie[1][i]) trie[1][i]=1;
        else fail[trie[1][i]]=1,q.push(trie[1][i]);
    }
    while(!q.empty()){
        int x=q.front();q.pop();
        sum[x]=val[x]+sum[fail[x]];
        fr(i,0,25){
            if(!trie[x][i]) trie[x][i]=trie[fail[x]][i];
            else fail[trie[x][i]]=trie[fail[x]][i],q.push(trie[x][i]);
        }
    }
}
int f[1010][4010],g[1010][4010];
ld db[1010][4010];
inline int ksm(int x,int y=mod-2){
    int sum=1;
    while(y){
        if(y&1) sum=1ll*sum*x%mod;
        y=y/2;x=1ll*x*x%mod;
    }return sum;
}
inline void add(int &x,int y){x+=y;if(x>=mod) x-=mod;}
const ld inf=1e15;
inline bool check(ld x){
    fr(i,0,n) fr(j,0,nod) f[i][j]=g[i][j]=0,db[i][j]=-inf;
    db[0][1]=0;
    // fr(i,0,25) cout<<trie[1][i]<<endl;
    fr(i,1,n){
        fr(j,1,nod) f[i][j]=f[i-1][j],g[i][j]=g[i-1][j],db[i][j]=db[i-1][j];
        int tp=getid(s[i]);
        fr(j,1,nod){
            if(!trie[j][tp]) continue;
            // cout<<j<<' '<<tp<<endl;
            int to=trie[j][tp];
            if(db[i][to]+eps<db[i-1][j]+sum[to]-x){
                // cout<<j<<' '<<to<<endl;
                db[i][to]=db[i-1][j]+sum[to]-x;
                f[i][to]=(f[i-1][j]+sum[to])%mod;
                g[i][to]=(g[i-1][j]+1);
            }
        }
    }
    fr(i,1,nod) if(db[n][i]>0) return true;
    return false;
}
inline void work(){
    n=read();q=read();scanf("%s",s+1);
    clear();
    while(q--){
        scanf("%s",c+1);int x=read();
        ins(x);
    }
    make_trie();
    ld l=0,r=1e9;
    fr(i,0,50){
        ld mid=(l+r)*0.5;
        if(check(mid)) l=mid;
        else r=mid;
    }
    check(l);
    // fr(i,1,nod) cout<<i<<' '<<db[n][i]<<endl;
    fr(i,1,nod) if(db[n][i]>0){
        int ans=1ll*f[n][i]*ksm(g[n][i])%mod;
        writeln(ans);return;
    }
}
inline void init(){
	int t=read();
	while(t--) work();
}
signed main(){
	// freopen("input.in","r",stdin);
	// freopen("output.out","w",stdout);
    init();
    // printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 899ms
memory: 70168kb

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:

332874949
599030808
249640519
332898173
665548146
81272
61572
67112
499196918
748779217
88888
831949361
74929
665552405
499139737
665543594
332830174
60785
748771076
63646
103574
101389
432700990
332787384
249703944
89874
110460
499215461
665540622
41750
782592345
96189
111031999
94537
83443
111657
...

result:

ok 80 lines

Test #2:

score: 0
Accepted
time: 815ms
memory: 49132kb

input:

80
6 9
ffdffd
df 5764
g 80673
dfd 93960
sje 2655
f 52989
dykez 24524
fd 93464
v 5951
dd 80386
4 8
hgig
hi 36540
eoy 56555
hgi 16024
da 39472
i 11615
w 28388
h 13233
b 36396
3 6
jhh
jhh 78310
jck 52810
h 93391
f 84166
j 58232
tixuja 90170
6 9
wvuuwu
uu 18802
cto 64653
v 17516
e 89244
vu 7176
yb 35851...

output:

499220334
30694
665604010
28868
499155869
399434929
43201
55925
53130
665596997
112603
48316
51377
665595637
332817598
92435
332790461
199775438
118514
46654
98424
99849
468077048
33082
499180262
499161147
99170
49530
249661952
665599894
19760011
31724
125134
665598061
665568382
53270
57347
74873729...

result:

ok 80 lines

Test #3:

score: 0
Accepted
time: 939ms
memory: 76208kb

input:

80
6 9
fdfdfd
f 62278
bd 63301
d 82508
hyx 79679
fd 77199
gat 3304
dd 38771
sz 65675
ffd 39030
3 4
ihi
i 23765
vum 4334
ihi 35317
jz 58824
7 12
nmnnnmn
mm 53554
z 37003
nnm 93166
os 47375
n 91295
k 23069
mn 70863
ke 53536
nmm 79577
xx 80568
nnn 9319
xpoioy 49090
3 4
stu
su 64015
pm 39855
stu 12774
g...

output:

249677224
665523851
499231655
499154184
95658
332794463
499226034
748789158
80649
144255
249638234
499158453
48294
249637902
90840
499168402
499173301
499254931
599034136
249676514
332790820
798677117
102014068
332796115
44767
96925
120384
499168015
88966
99986
27092909
66644
832034701
125185
332785...

result:

ok 80 lines

Test #4:

score: 0
Accepted
time: 986ms
memory: 78444kb

input:

80
7 12
jihijhj
i 72948
zl 98729
hjh 84734
r 57189
hh 75314
yn 6123
jh 73861
u 47490
jhj 86000
vi 60571
j 69646
rzeaekb 51127
3 6
rrr
r 47818
c 7921
rr 7750
aq 79014
rrr 28438
igv 43270
6 8
jkijkj
k 23174
xs 49356
j 68561
xw 31342
kk 97855
flt 49385
kij 10232
xy 84681
6 8
klkmml
kl 29970
chw 4110
lk...

output:

399420420
62464
499194278
499164004
665521173
499197753
332808457
44186
665604467
97158
831965037
665545040
665623667
97887
665633265
58787
249631705
90776
332775392
75134
18376
798687225
25718328
332778936
47597
121972
44629
665528383
499213995
748819760
73157720
80450
698915455
249699722
499140797...

result:

ok 80 lines

Test #5:

score: 0
Accepted
time: 946ms
memory: 59644kb

input:

80
3 4
bbb
b 44630
kt 93484
bb 29609
ps 66781
6 8
ttttut
ttu 29879
lh 47140
tu 38786
lel 2159
ttt 63824
e 42744
tt 84477
a 135
8 13
qqqqqpoo
po 18783
pt 34823
qpo 1940
mh 66501
qq 68822
tdr 21257
o 59431
fxh 58424
q 18175
or 99541
p 43971
tqnmjr 34605
qqq 92858
4 8
uwvv
v 86612
ig 38634
uv 21955
c 5...

output:

332812487
105876
199777818
102222
70120
332840767
79049
332835764
54271
99337
460834412
665570126
56347
99619
30473
499283122
499229142
499208844
49412
665558243
748735960
332866356
737994449
499188739
499230438
34815
332868289
665538255
332834232
94334
244225146
499231229
844832371
199748229
108136...

result:

ok 80 lines