40分,悬关!
查看原帖
40分,悬关!
816792
coritom楼主2024/8/17 10:33
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e6 + 5;
int n, node[maxn][26], exist[maxn], total;
int nt[maxn], Map[maxn], ans[maxn];
string s;
void insert(string s, int p){
	int u = 0, len = s.size();
	for(int i = 0; i < len; i++){
		int c = s[i] - 'a';
		if(node[u][c] == 0) node[u][c] = ++total;
		u = node[u][c];
	}
	Map[u] = p;
	exist[u]++;
}
void get_next(){
	queue<int> q;
	for(int i = 0; i < 26; i++){
		if(node[0][i]) q.push(node[0][i]); 
	}
	while(!q.empty()){
		int x = q.front();
		q.pop();
		for(int i = 0; i < 26; i++){
			if(node[x][i]){
				nt[node[x][i]] = node[nt[x]][i];
				q.push(node[x][i]);
			}
			else node[x][i] = node[nt[x]][i];
		}
	}
}
void query(){
	int u = 0;
	for(int i = 0; s[i]; i++){
		int c = s[i] - 'a';
		u = node[u][c];
		int x = u;
		while(x){
			ans[Map[x]] += exist[x];
			x = nt[x];
		}
	}
}
signed main(){
	ios_base::sync_with_stdio(false);
	cout.tie(0), cin.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> s;
		insert(s, i);
	}
	get_next();
	cin >> s;
	query();
	for(int i = 1; i <= n; i++){
		cout << ans[i] << '\n';
	}
	return 0;
}

请各位巨佬帮忙看一下。

2024/8/17 10:33
加载中...