QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#505064#9111. Zayin and StringuntitledtwoWA 220ms64132kbC++172.9kb2024-08-04 19:16:132024-08-04 19:16:13

Judging History

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

  • [2024-08-04 19:16:13]
  • 评测
  • 测评结果:WA
  • 用时:220ms
  • 内存:64132kb
  • [2024-08-04 19:16:13]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
const int INF = 0x3f3f3f3f;
const int mods = 998244353;
const double eps = 1e-5;
typedef long long ll;

int n, m, nodenum;
int ch[MAXN][26], fa[MAXN];
char st[MAXN], ST[MAXN];
ll A[MAXN], s[MAXN][26];
queue<int> que;

void Init() {
	for (int i = 1; i <= nodenum ; ++ i) {
		fa[i] = A[i] = 0;
		for (int j = 0; j < 26 ; ++ j) ch[i][j] = s[i][j] = 0;
	}
	nodenum = 1;
}
int quick_pow(int x, int y) {
	int ret = 1;
	for (; y ; y >>= 1) {
		if (y & 1) ret = 1ll * ret * x % mods;
		x = 1ll * x * x % mods;
	}
	return ret;
}
void insert(char *st, int c) {
	int len = strlen(st), nw = 1;
	for (int i = 0; i < len ; ++ i)  {
		if (!ch[nw][st[i] - 'a']) ch[nw][st[i] - 'a'] = ++ nodenum;
		nw = ch[nw][st[i] - 'a'];
	}
	A[nw] += c;
}
void Build() {
	for (int i = 1; i <= nodenum ; ++ i)
		for (int j = 0; j < 26 ; ++ j)
			if (ch[i][j]) s[i][j] = A[ch[i][j]];
	fa[1] = 1;
	for (int i = 0; i < 26 ; ++ i)
		if (ch[1][i]) fa[ch[1][i]] = 1, que.push(ch[1][i]);
		else ch[1][i] = 1;
	while (!que.empty()) {
		int q = que.front(); que.pop();
		for (int i = 0; i < 26 ; ++ i) {
			if (ch[q][i]) fa[ch[q][i]] = ch[fa[q]][i], que.push(ch[q][i]);
			else ch[q][i] = ch[fa[q]][i];
			s[q][i] += s[fa[q]][i];
		}
	}
//	for (int i = 1; i <= nodenum ; ++ i)
//		for (int j = 0; j < 26 ; ++ j) 
//			if (s[i][j]) cout << i << " " << j << " " << ch[i][j] << " " << s[i][j] << endl;
}
double f[1005][4005];
int d[1005][4005];
ll g[1005][4005];

int check(double x) {
	for (int j = 1; j <= nodenum ; ++ j) f[0][j] = -1e60, g[0][j] = d[0][j] = 0;
	f[0][1] = 0;
	for (int i = 0; i < n ; ++ i) {
		for (int j = 1; j <= nodenum ; ++ j) {
			f[i + 1][j] = f[i][j];
			g[i + 1][j] = g[i][j];
			d[i + 1][j] = d[i][j];
		}
		for (int j = 1; j <= nodenum ; ++ j)
			if (f[i + 1][ch[j][st[i + 1] - 'a']] < f[i][j] + s[j][st[i + 1] - 'a'] - x) {
				f[i + 1][ch[j][st[i + 1] - 'a']] = f[i][j] + s[j][st[i + 1] - 'a'] - x; 
				g[i + 1][ch[j][st[i + 1] - 'a']] = g[i][j] + s[j][st[i + 1] - 'a']; 
				d[i + 1][ch[j][st[i + 1] - 'a']] = d[i][j] + 1;
			}
	}
//	for (int i = 1; i <= nodenum ; ++ i) 
//		cout << i << " " << f[n][i] << " " << d[n][i] << endl;
	for (int i = 1; i <= nodenum ; ++ i)
		if (f[n][i] > eps) return 1;
	return 0;
}

signed main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	int Case;
	scanf("%d", &Case);
	while (Case --) {
		Init();
		scanf("%d%d", &n, &m);
		scanf("%s", st + 1);
		for (int i = 1; i <= m ; ++ i) {
			int x;
			scanf("%s%d", ST, &x);
			insert(ST, x);
		}
		Build();
		double l = 0, r = 100000;
		while (l + eps < r) {
			double mid = (l + r) * 0.5;
			if (check(mid)) l = mid;
			else r = mid;
		}
		check(l);

		int ans = 0;
		for (int i = 1; i <= nodenum ; ++ i)
			if (f[n][i] > eps) { ans = 1ll * g[n][i] * quick_pow(d[n][i], mods - 2) % mods; break; }
		printf("%d\n", ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 220ms
memory: 64132kb

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
499234718
665548146
81272
61572
67112
499196918
748779217
88888
831949361
74929
665552405
499139737
665543594
332830174
60785
748771076
63646
103574
101389
417292243
332787384
499236773
89874
110460
499215461
665540622
41750
312105660
96189
101652
94537
83443
111657
665...

result:

wrong answer 4th lines differ - expected: '332898173', found: '499234718'