QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#621499#9111. Zayin and StringSGColinWA 220ms68704kbC++203.2kb2024-10-08 14:59:182024-10-08 14:59:18

Judging History

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

  • [2024-10-08 14:59:18]
  • 评测
  • 测评结果:WA
  • 用时:220ms
  • 内存:68704kb
  • [2024-10-08 14:59:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

#define eb emplace_back
#define all(s) (s).begin(), (s).end()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)

const int N = 4007;
const int M = 1007;
const int mod = 998244353;

template<typename T>
inline bool getmax(T &a, T b) {return (a < b ? (a = b, true) : false);}

inline int fpow(int x, int t = mod - 2) {
	int res = 1;
	for (; t; t >>= 1, x = 1ll * x * x % mod)
		if (t & 1) res = 1ll * res * x % mod;
	return res;
}

struct Trie_Graph {
	int rt = 0, cnt = 0, fail[N];
	int son[N][26], nxt[N][26];
	// son = structure of the Trie
	// nxt = fill the trie in to trie graph
	int w[N];
	void reset() {
		rt = cnt = 0;
		memset(son[0], 0, sizeof(son[0]));
		memset(nxt[0], 0, sizeof(nxt[0]));
	}
	int newnode() {
		++cnt;
		fail[cnt] = w[cnt] = 0;
		memset(son[cnt], 0, sizeof(son[cnt]));
		memset(nxt[cnt], 0, sizeof(nxt[cnt]));
		return cnt;
	}
	int insert(int pre, int ch) {
		return son[pre][ch] ? son[pre][ch] : son[pre][ch] = newnode();
	}
	void insert(string &S) {
		int nw = rt;
		for (auto &ch : S) nw = insert(nw, ch - 'a');
		cin >> w[nw];
	}
	void build() {
		queue<int> Q;
		Q.push(rt);
		memcpy(nxt[rt], son[rt], sizeof(son[rt]));
		while (!Q.empty()) {
			int u = Q.front(); Q.pop();
			rep(ch, 0, 25) {
				int v = son[u][ch];
				if (!v) continue; 
				fail[v] = (u == 0 ? 0 : nxt[fail[u]][ch]);
				w[v] += w[fail[v]];
				memcpy(nxt[v], nxt[fail[v]], size(nxt[fail[v]]));
				rep(cc, 0, 25) if (son[v][cc]) nxt[v][cc] = son[v][cc];
				Q.push(v);
			}
		}
	}

	double mx[M][N];

	int cur[M][N];

	pii pre[M][N], anspos;

	inline bool check(double val, string &S) {
		mx[0][rt] = cur[0][rt] = 0;
		rep(i, 1, cnt) mx[0][i] = -1e18, cur[0][i] = 0, pre[0][i] = {0, 0};
		int pos = 0;
		for (auto &ch : S) {
			int x = ch - 'a';
			rep(u, 0, cnt) {
				mx[pos + 1][u] = mx[pos][u];
				cur[pos + 1][u] = cur[pos][u];
				pre[pos + 1][u] = pre[pos][u];
			}
			rep(u, 0, cnt) {
				int v = nxt[u][x];
				if (getmax(mx[pos + 1][v], mx[pos][u] + w[v] - val)) {
					cur[pos + 1][v] = pos + 1;
					pre[pos + 1][v] = {cur[pos][u], u};
					if (mx[pos + 1][v] > 1e-9) {anspos = {pos + 1, v}; return true;}
				}
			}
			++pos;
		}
		return false;
	}

	inline int run(string &S) {
		int sum = 0, nw = rt;
		for (auto &ch : S) {
			nw = nxt[nw][ch - 'a'];
			sum = (sum + w[nw]) % mod;
		}
		return 1ll * sum * fpow(S.length()) % mod;
	}

} tg;



inline void work() {
	
	tg.reset();
	int n, m; cin >> n >> m;
	string S, T; cin >> S;
	rep(i, 1, m) {cin >> T; tg.insert(T);}
	tg.build();

	double l = 0, r = 1e8;
	rep(i, 1, 50) {
		double mid = (l + r) / 2;
		tg.check(mid, S) ? l = mid : r = mid;
	}
	tg.check(l, S);

	string ANS = "";
	auto [pos, cur] = tg.anspos;
	do {
		ANS += S[pos - 1];
		auto [_pos, _cur] = tg.pre[pos][cur];
		swap(pos, _pos);
		swap(cur, _cur);
	} while (pos);

	reverse(all(ANS));

	cout << tg.run(ANS) << endl;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int t; cin >> t;
	while (t--) work();
	return 0;
}

详细

Test #1:

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

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
66211
77126
136709
665548146
81272
61572
67112
73114
95033
88888
665557674
74929
665552405
499139737
665543594
332830174
60785
499200076
665552083
103574
101389
432700990
33308
249703944
89874
94158
499215461
665540622
41750
332897189
499193200
499208480
94537
83443
111657
665560098
21918
...

result:

wrong answer 2nd lines differ - expected: '599030808', found: '66211'