QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#549388#9111. Zayin and StringwangyangjenaTL 0ms0kbC++142.6kb2024-09-06 15:02:272024-09-06 15:02:28

Judging History

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

  • [2024-09-06 15:02:28]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-09-06 15:02:27]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define double long double
#pragma GCC optimize(2)
using namespace std;
const int N = 1e5 + 10, MOD = 998244353, INF = 1e9;
const double eps = 1e-10;

int Q_pow(int a, int b){
    int ans = 1, p = a;
    while(b){
        if(b & 1) ans = (ans * p) % MOD;
        b >>= 1;
        p = (p * p) % MOD;
    }
    return ans;
}

int t;
int n, m, val;
string s, ss;

struct Node{
	int son[26];
	int fail, ed;
}tr[N];
int cnt, sum[N], v[N][26];
queue <int> q;

void Insert(string s, int v){
	int p = 0;
	for(auto i : s){
		int ch = i - 'a';
		if(!tr[p].son[ch]) tr[p].son[ch] = ++cnt;
		p = tr[p].son[ch];
	}
	tr[p].ed += v;
}

void Get_fail(){
	for(int i = 0; i < 26; i++){
		if(tr[0].son[i]){
			q.push(tr[0].son[i]);
		}
	}

	while(!q.empty()){
		int tp = q.front();
		q.pop();

		sum[tp] = sum[tr[tp].fail] + tr[tp].ed;
		for(int i = 0; i < 26; i++){
			if(tr[tp].son[i]){
				tr[tr[tp].son[i]].fail = tr[tr[tp].fail].son[i];
				q.push(tr[tp].son[i]);
			}else{
				tr[tp].son[i] = tr[tr[tp].fail].son[i];
			}
		}
	}
}

double f[1001][4001];
int dp[1001][4001], len[1001][4001];

bool Check(double lim){

	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= cnt; j++){
			f[i][j] = -INF;
			dp[i][j] = 0;
			len[i][j] = 0;
		}
	}
	
	f[0][0] = 0;
	dp[0][0] = 0;
	len[0][0] = 0;

	for(int i = 0; i < n; i++){
		for(int j = 0; j <= cnt; j++){
			f[i + 1][j] = f[i][j];
			dp[i + 1][j] = dp[i][j];
			len[i + 1][j] = len[i][j];
		}
		for(int j = 0; j <= cnt; j++){
			int ch = s[i] - 'a';
			int to = tr[j].son[ch];
			if(!to) continue;
			if(f[i + 1][to] + eps < f[i][j] + sum[to] - lim){
				f[i + 1][to] = f[i][j] + tr[to].ed - lim;
				dp[i + 1][to] = dp[i][j] + tr[to].ed;
				len[i + 1][to] = len[i][j] + 1;
			}
		}
	}

	for(int i = 1; i <= cnt; i++){
		if(f[n][i] >= 0){
			// printf("%.4Lf\n", f[n][i]);
			return 1;
		}
	}
	return 0;
}

signed main(){
	// freopen("1.in", "r", stdin);

	cin >> t;

	while(t--){
		cin >> n >> m >> s;
		
		for(int i = 1; i <= m; i++){
			cin >> ss >> val;
			Insert(ss, val);
		}

		double l = 0, r = 1e9, ans = 0;

		for(int i = 1; i <= 50; i++){
			double mid = (l + r) / 2;
			// printf("%.4Lf l %.4Lf r %.4Lf mid %d\n", l, r, mid, Check(mid));
			if(Check(mid)){
				ans = mid;
				l = mid + 1;
			}else{
				r = mid - 1;
			}
		}

		// printf("%.4Lf\n", ans);
		Check(ans);
		
		for(int i = 1; i <= cnt; i++){
			if(f[n][i] >= 0){
				int ret = dp[n][i] * Q_pow(len[n][i], MOD - 2) % MOD;
				cout << ret << "\n";
			}
		}

		for(int i = 0; i <= cnt; i++){
			memset(tr[i].son, 0, sizeof(tr[i].son));
			tr[i].ed = tr[i].fail = 0;
			sum[i] = 0;
		}
	}
}

详细

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:

92946
499172278
499198100
96670
332788889
81272
61572
67112
69859
95033
88888
332784672
74929
38759
17290
33737
76430
60785
58076
665552083
78051
77523
499194475
33308
95383
89874
71532
90741
499160996
41750
499216047
499193200
499184393
94537
83443
93595
499175237
332766789
62015
332800788
59111
66...

result: