QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#546279#9111. Zayin and StringhhoppitreeWA 558ms4568kbC++172.7kb2024-09-03 22:06:572024-09-03 22:06:57

Judging History

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

  • [2024-09-03 22:06:57]
  • 评测
  • 测评结果:WA
  • 用时:558ms
  • 内存:4568kb
  • [2024-09-03 22:06:57]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 1005, M = 4005, P = 998244353;

int tot, ch[26][M], fail[M], sum[M];
long double f[M], g[M];
vector<int> T[M];

void dfs(int x) {
    for (auto v : T[x]) sum[v] += sum[x], dfs(v);
}

signed main() {
    int te; scanf("%d", &te);
    while (te--) {
        int n, m; scanf("%d%d", &n, &m);
        string S; cin >> S;
        S = ' ' + S;
        for (int i = 0; i <= tot; ++i) {
            fail[i] = sum[i] = 0;
            for (int j = 0; j < 26; ++j) ch[j][i] = 0;
        }
        tot = 0;
        for (int i = 1; i <= m; ++i) {
            string str;
            cin >> str;
            int now = 0;
            for (auto c : str) {
                c -= 'a';
                if (!ch[c][now]) ch[c][now] = ++tot;
                now = ch[c][now];
            }
            int val; scanf("%d", &val);
            sum[now] += val;
        }
        queue<int> Q;
        for (int i = 0; i < 26; ++i) {
            if (ch[i][0]) Q.push(ch[i][0]);
        }
        while (!Q.empty()) {
            int x = Q.front(); Q.pop();
            for (int i = 0; i < 26; ++i) {
                if (ch[i][x]) fail[ch[i][x]] = ch[i][fail[x]], Q.push(ch[i][x]);
                else ch[i][x] = ch[i][fail[x]];
            }
        }
        for (int i = 0; i <= tot; ++i) T[i].clear();
        for (int i = 1; i <= tot; ++i) T[fail[i]].push_back(i);
        dfs(0);
        long double L = 0, R = 1e8, res = 0;
        while (fabs(L - R) > 1e-10) {
            long double mid = (L + R) / 2;
            for (int j = 0; j <= tot; ++j) f[j] = -1e18;
            f[0] = 0;
            for (int i = 1; i <= n; ++i) {
                for (int j = 0; j <= tot; ++j) g[j] = f[j];
                for (int j = 0; j <= tot; ++j) {
                    int k = S[i] - 'a';
                    f[ch[k][j]] = max(f[ch[k][j]], g[j] + sum[ch[k][j]] - mid);
                }
            }
            int flg = 0;
            for (int j = 0; j <= tot; ++j) {
                flg |= (f[j] > 1e-10);
            }
            if (flg) {
                res = mid, L = mid;
            } else {
                R = mid;
            }
        }
        for (int i = 1; i <= n; ++i) {
            long double v = res * i;
            if ((int)floor(v + 1e-9) != (int)floor(v - 1e-9)) {
                int res = (int)floor(v + 1e-9), x = i, y = P - 2;
                while (y) {
                    if (y & 1) res = 1ll * res * x % P;
                    x = 1ll * x * x % P;
                    y >>= 1;
                }
                printf("%d\n", res);
                break;
            }
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 558ms
memory: 4568kb

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
96189
111031999
94537
83443
111657
665560098
...

result:

wrong answer 31st lines differ - expected: '782592345', found: '96189'