QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#563725 | #9111. Zayin and String | juan_123 | WA | 522ms | 67868kb | C++14 | 2.6kb | 2024-09-14 15:28:33 | 2024-09-14 15:28:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
const int mod = 998244353;
const double inf = 1e18;
int qp(int p,int q){
int ans = 1,pro = p;
while(q){
if(q&1)ans = ans*pro%mod;
pro = pro*pro%mod;q>>=1;
}
return ans;
}
int n,m;
char s[10005];int nw;
int trie[10005][26],fail[10005],v[10005];
vector<int>p[10005];
void ins(string s,int c){
int now =0 ;
for(int i =0;i<s.size();i++){
if(!trie[now][s[i]-'a'])trie[now][s[i]-'a'] = ++nw;
now = trie[now][s[i]-'a'];
}
v[now]+=c;
}
void dfs(int now,int fa){
if(now!=fa)v[now]+=v[fa];
for(auto x:p[now])dfs(x,now);
}
void build(){
queue<int>q;
for(int i =0;i<26;i++)if(trie[0][i])q.push(trie[0][i]);
while(!q.empty()){
int now = q.front();q.pop();
for(int i =0;i<26;i++){
if(trie[now][i]){
q.push(trie[now][i]);
fail[trie[now][i]] = trie[fail[now]][i];
}else{
trie[now][i] = trie[fail[now]][i];
}
}
}
for(int i =1;i<=nw;i++)p[fail[i]].push_back(i);
dfs(0,0);
}
double dp[1005][4005];int pre[1005][4005];
bool check(double x){
for(int i =0;i<=n;i++)for(int j =0;j<=nw;j++)dp[i][j] = -inf;
dp[0][0] = 0;
for(int i = 0;i<n;i++){
for(int j =0;j<=nw;j++){
if(dp[i][j] == -inf)continue;
if(dp[i+1][j]<dp[i][j]){
dp[i+1][j] = dp[i][j];pre[i+1][j] = j;
}
int c = s[i+1]-'a',to = trie[j][c];
if(dp[i+1][to]<dp[i][j]+v[to]-x){
dp[i+1][to]=dp[i][j]+v[to]-x;
pre[i+1][to] = j;
}
}
}
double mx = -inf;
for(int i =0;i<=nw;i++)mx = max(mx,dp[n][i]);
return (mx>0);
}
void solve(){
for(int i =0;i<=nw;i++)fail[i] = 0,v[i] =0,p[i].clear();
for(int i =0;i<=nw;i++)for(int j =0;j<26;j++)trie[i][j] = 0;
nw = 0;
cin >> n >> m;for(int i = 1;i<=n;i++)cin >> s[i];
int sum = 0;
for(int i = 1;i<=m;i++){
string s;cin >> s;int c;cin >> c;
ins(s,c);sum+=c;
}
build();
double l = 0,r = sum;
for(int i =0;i<60;i++){
double mid = (l+r)/2;
if(check(mid))l = mid;
else r = mid;
}
check(l);
int tot=0,s = 0;
double mx =0;int mxpos = 0;
for(int i =0;i<=nw;i++)if(dp[n][i]>mx)mx =dp[n][i],mxpos = i;
for(int i = n;i>=1;i--){
int to = pre[i][mxpos];
if(mxpos != to)++tot,s+=v[mxpos];
mxpos = to;
}
s%=mod;
// cout << tot << " " << s << endl;
cout << s*qp(tot,mod-2)%mod << endl;
}
signed main(){
int t;cin >> t;
while(t--){
solve();
}
return 0;
}/*
3
17 3
woyaoakccpcxiamen
ak 5
ccpc 6
xiamen 8
33 3
niweishenmezhengtianxunlianbuliwo
wo 3
se 1
zayin 7
39 2
programmingcontestandmewhichisimportant
me 11
gg 2
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 522ms
memory: 67868kb
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:
118360 73525 249640519 136709 665548146 81272 61572 67112 73114 748779217 88888 665557674 74929 665552405 499139737 665543594 332830174 60785 499200076 63646 103574 101389 432700990 332787384 133436 89874 94158 499215461 665540622 41750 782592345 96189 499208480 94537 83443 111657 665560098 21918 70...
result:
wrong answer 1st lines differ - expected: '332874949', found: '118360'