从加强版板子贺过来的代码挂了?
查看原帖
从加强版板子贺过来的代码挂了?
482610
Mortidesperatslav楼主2024/11/4 10:35

MLE+RE+AC。

#include<bits/stdc++.h>
using namespace std;
string ss[505];
int n, cnt, tot, id[505];
struct node{
	int c[30], deg, fail, res, id;
}tr[505];
int ans[505]; 
void ins(string s, int &tmp){
	s = " " + s;
	int u = 0;
	for (int i = 1; i < s.size(); i++){
		int vl = s[i] - 'a';
		if (!tr[u].c[vl])
			tr[u].c[vl] = ++cnt;
		u = tr[u].c[vl];
	}
	if (!tr[u].id)
		tr[u].id = ++tot, tmp = tot;
	else
		tmp = tr[u].id;
}
void build(){
	queue<int> q;
	for (int i = 'a'; i <= 'z'; i++)
		if (tr[0].c[i - 'a'])
			q.push(tr[0].c[i - 'a']);
	while (!q.empty()){
		int u = q.front();
		q.pop();
		for (int i = 'a'; i <= 'z'; i++){
			if (tr[u].c[i - 'a']){
				tr[tr[u].c[i - 'a']].fail = tr[tr[u].fail].c[i - 'a'];
				tr[tr[tr[u].fail].c[i - 'a']].deg++;
				q.push(tr[u].c[i - 'a']);
			}else{
				tr[u].c[i - 'a'] = tr[tr[u].fail].c[i - 'a'];
			}
		}
	} 
}
void qry(string s){
	int u = 0;
	s = " " + s;
	for (int i = 1; i < s.size(); i++){
		u = tr[u].c[s[i] - 'a'];
		tr[u].res++;
	}
}
void stat(){
	queue<int> q;
	for (int i = 1; i <= cnt; i++)
		if (!tr[i].deg)
			q.push(i);
	while (!q.empty()){
		int u = q.front();
		q.pop();
		ans[tr[u].id] = tr[u].res;
		tr[tr[u].fail].res += tr[u].res;
		tr[tr[u].fail].deg--;
		if (!tr[tr[u].fail].deg)
			q.push(tr[u].fail); 
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	while (1){
		cin >> n;
		if (n == 0)
			break;
		memset(id, 0, sizeof id);
		memset(ans, 0, sizeof ans);
		cnt = tot = 0;
		memset(tr[0].c, 0, sizeof tr[0].c);
		tr[0].deg = tr[0].fail = tr[0].res = tr[0].id = 0;
		for (int i = 1; i <= n; i++){
			cin >> ss[i];
			ins(ss[i], id[i]);
			memset(tr[i].c, 0, sizeof tr[i].c);
			tr[i].deg = tr[i].fail = tr[i].res = tr[i].id = 0;
		}
		build();
		string qwq;
		cin >> qwq;
		qry(qwq);
		stat();
		int mx = 0;
		for (int i = 1; i <= n; i++)
			mx = max(mx, ans[id[i]]);
		cout << mx << "\n";
		for (int i = 1; i <= n; i++)
			if (ans[id[i]] == mx)
				cout << ss[i] << "\n";
	}
	return 0;
}
2024/11/4 10:35
加载中...