QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#546677#9111. Zayin and StringwangyangjenaWA 3ms4024kbC++142.3kb2024-09-04 11:32:232024-09-04 11:32:24

Judging History

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

  • [2024-09-04 11:32:24]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:4024kb
  • [2024-09-04 11:32:23]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define double long double
#pragma GCC optimize(2)
using namespace std;
const int N = 1e5 + 10, MOD = 998244353;

int Q_pow(int a, int b){
    int ans = 1, p = a;
    while(b){
        if(b & 1) ans = (ans * p) % MOD;
        b >>= 1;
        p = (p * p) % MOD;
    }
    return ans;
}

int t;
int n, m, val[N];
string s, dict;

struct Node{
    int son[26];
    int ed, fail;
}tr[N];
int cnt = 1;
queue <int> q;

void Insert(string s, int id){
    int p = 0;
    for(auto i : s){
        int ch = i - 'a';
        if(!tr[p].son[ch]) tr[p].son[ch] = ++cnt;
        p = tr[p].son[ch];
    }
    tr[p].ed = id;
}

void Get_fail(){
    for(int i = 0; i < 26; i++){
        if(tr[1].son[i]){
            q.push(tr[1].son[i]);
        }
    }
    while(!q.empty()){
        int tp = q.front();
        q.pop();

        for(int i = 0; i < 26; i++){
            if(tr[tp].son[i]){
                tr[tr[tp].son[i]].fail = tr[tr[tp].fail].son[i];
                q.push(tr[tp].son[i]);
            }else{
                tr[tp].son[i] = tr[tr[tp].fail].son[i];
            }
        }
    }
}

double maxi;
int ans;

void Query(int st){
    int p = 0, sum = 0;
    for(int i = st; i < s.size(); i++){
        int ch = s[i] - 'a';
        p = tr[p].son[ch];
        
        int tmp = p;
        while(tmp){
            sum += val[tr[tmp].ed];
            tmp = tr[tmp].fail;
        }

        int len = i - st + 1;
        double v = 1.0 * sum / len;

        // cout << st << "+" << i << " " << sum << " ";
        // printf("%.10Lf\n", v);

        if(v > maxi){
            maxi = v;
            ans = sum * Q_pow(len, MOD - 2) % MOD;
        }
    }
}

void Reset(){
    maxi = 0, ans = 0;

    for(int i = 1; i <= cnt; i++){
        memset(tr[i].son, 0, sizeof(tr[i].son));
        tr[i].ed = tr[i].fail = 0;
    }
    cnt = 1;
}

signed main(){
    // freopen("1.in", "r", stdin);

    cin >> t;
    while(t--){
        cin >> n >> m >> s;
        for(int i = 1; i <= m; i++){
            cin >> dict >> val[i];
            Insert(dict, i);
        }
        Get_fail();

        for(int i = 0; i < s.size(); i++){
            Query(i);
        }
        cout << ans << "\n";

        Reset();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 4024kb

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
499172278
499198100
499199414
98405
96243
61572
67112
85102
95033
88888
332784672
74929
77518
56881
74971
87511
62175
96466
499202275
499197679
85110
499198000
33308
58231
89874
71532
90741
67717
39445
91499
60826
499191874
94537
83443
93595
78766
399347004
62015
499147259
59111
44053
88812
73...

result:

wrong answer 1st lines differ - expected: '332874949', found: '92946'