QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#532677#9111. Zayin and StringZYLZPPWA 146ms52312kbC++202.7kb2024-08-25 08:49:312024-08-25 08:49:32

Judging History

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

  • [2024-08-25 08:49:32]
  • 评测
  • 测评结果:WA
  • 用时:146ms
  • 内存:52312kb
  • [2024-08-25 08:49:31]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define For(i, l, r) for (int i = (l); i <= (r); ++i)
typedef long long ll;
typedef long double Lf;
const int N = 1e3 + 5, S = 4e3 + 5, Mo = 998244353;
const Lf eps = 1e-10;
inline int ml(const int &x, const int &y) { return 1ll * x * y % Mo; }
inline void mul(int &x, const int &y) { x = ml(x, y); }
inline int qPow(int a, int b) { int r=1; for (;b;b>>=1,mul(a,a)) if (b&1) mul(r,a); return r; }
template<class C> inline bool cmax(C &x, const C &y) { return y>x? x=y, 1: 0; }

struct IO {
	char c; int f;
#define gc() (getchar())
	template<class C>
	inline IO& operator >> (C &x) {
		x = 0; f = 1;
		while (!isdigit(c = gc()) && ~c) f |= -!(c ^ 45);
		while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = gc();
		x *= f; return *this;
	}
	inline bool operator ~ () { return ~c; }
} io;

int T, n, m, vis[N][S], tim;
Lf f[N][S];
char t[N];

namespace ACAM {
    int ch[S][26], tot = 1, fail[S], sum[S];
    char s[S];

    inline void add(char s[], int x) {
        int n = strlen(s + 1), u = 1;
        For (i, 1, n) {
            int c = s[i] - 'a';
            if (!ch[u][c]) ch[u][c] = ++tot;
            u = ch[u][c];
        }
        sum[u] += x;
    }

    inline void build() {
        For (i, 0, 25) ch[0][i] = 1;
        queue<int > q; q.push(1);
        fail[1] = 0;
        while (!q.empty()) {
            int u = q.front(); q.pop(); sum[u] += sum[fail[u]];
            For (i, 0, 25) (ch[u][i]? q.push(ch[u][i]), fail[ch[u][i]]: ch[u][i]) = ch[fail[u]][i];
        }
    }
}
using namespace ACAM;

inline bool ok(Lf x) {
    f[0][1] = 0; vis[0][1] = ++tim;
    For (i, 0, n - 1) For (j, 1, tot) if (vis[i][j] == tim) {
        int c = t[i + 1] - 'a', k = ch[j][c];
        if (vis[i + 1][j] ^ tim) vis[i + 1][j] = tim, f[i + 1][j] = f[i][j];
        else cmax(f[i + 1][j], f[i][j]);
        if (vis[i + 1][k] ^ tim) vis[i + 1][k] = tim, f[i + 1][k] = f[i][j] + sum[k] - x;
        else cmax(f[i + 1][k], f[i][j] + sum[k] - x);
    }
    For (i, 1, tot) if (vis[n][i] == tim && f[n][i] > eps) return 1;
    return 0;
}

inline void solve() {
    For (i, 1, tot) fail[i] = sum[i] = 0, memset(ch[i], 0, sizeof ch[i]);
    tot = 1;
    io >> n >> m;
    scanf("%s", t + 1);
    int x;
    For (i, 1, m) scanf("%s", s + 1), io >> x, add(s, x);
    build();
    Lf l = 0, r = 2e8;
    while (r - l > eps) {
        Lf mid = (l + r) / 2;
        if (ok(mid)) l = mid;
        else r = mid;
    }
    For (i, 1, n) {
        ll x = round(l * i);
        if (fabs(x - l * i) < n * eps) { printf("%d\n", ml(x % Mo, qPow(i, Mo - 2))); return; }
    }
}

int main() {
    io >> T;
    while (T--) solve();

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 146ms
memory: 52312kb

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: -100
Wrong Answer
time: 142ms
memory: 34984kb

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
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
748737298
96885
14...

result:

wrong answer 3rd lines differ - expected: '665604010', found: '28868'