QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#546275#9111. Zayin and StringhhoppitreeTL 0ms0kbC++173.1kb2024-09-03 22:03:242024-09-03 22:03:24

Judging History

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

  • [2024-09-03 22:03:24]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-09-03 22:03:24]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

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

int nxt[26][N], tot, ch[26][M], fail[M], sum[M];
long double f[N][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 str; cin >> str;
        str = ' ' + str;
        for (int i = 0; i < 26; ++i) nxt[i][n + 1] = n + 1;
        for (int i = n; i; --i) {
            for (int j = 0; j < 26; ++j) nxt[j][i] = nxt[j][i + 1];
            nxt[str[i] - 'a'][i] = i;
        }
        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) {
            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 i = 1; i <= n + 1; ++i) {
                for (int j = 0; j <= tot; ++j) f[i][j] = -1e18;
            }
            f[1][0] = 0;
            for (int i = 1; i <= n; ++i) {
                for (int j = 0; j <= tot; ++j) {
                    for (int k = 0; k < 26; ++k) {
                        if (nxt[k][i] > n) continue;
                        f[nxt[k][i] + 1][ch[k][j]] = max(f[nxt[k][i] + 1][ch[k][j]], f[i][j] + sum[ch[k][j]] - mid);
                    }
                }
            }
            int flg = 0;
            for (int i = 1; i <= n + 1; ++i) {
                for (int j = 0; j <= tot; ++j) {
                    flg |= (f[i][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
Time Limit Exceeded

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: