WA #2 求助
查看原帖
WA #2 求助
819212
_fewq楼主2023/10/10 19:24

https://www.luogu.com.cn/record/128491428

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+114;
int w9hish92;
#define new_nd() (++w9hish92)
int ch[N][26],fa[N],nxt[N],cnt[N],b[N],rt=new_nd(),n,ans;
string s;
void ins(string& s){
	int u=rt;
	for(char c:s){
		if(!ch[u][c-'a']) fa[ch[u][c-'a']=new_nd()]=u/*,cout << u << " -> " << c << " -> " << ch[u][c-'a'] << endl*/;
		u=ch[u][c-'a'];
	}
	++cnt[u];
}
queue<pair<int,int> > q;
void bfs(){
	nxt[rt]=0;
	for(int i=0;i<26;++i) if(ch[rt][i]) q.push(pair<int,int>(ch[rt][i],i));
	while(!q.empty()){
		int u=q.front().first,w=q.front().second;
		q.pop();
		int v=nxt[fa[u]];
		while(!ch[v][w]) v=nxt[u];
		nxt[u]=ch[v][w];
		for(int i=0;i<26;++i) if(ch[u][i]) q.push(pair<int,int>(ch[u][i],i));
	}
}
int main(){
	std::ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	for(int i=0;i<26;++i) ch[0][i]=rt;
	cin >> n;
	for(int i=1;i<=n;++i) cin >> s,ins(s);
	bfs();
	cin >> s;
	int u=rt;
//	for(int i=1;i<=w9hish92;++i) cout << i << ":" << nxt[i] << endl;
	for(char c:s){
		int w=c-'a';
		while(!ch[u][w]) u=nxt[u];
		int v=u=ch[u][w];
		while(v && !b[v]) ans+=cnt[v],b[v]=1,v=nxt[v];
	}
	cout << ans << endl;
	return 0;
}
2023/10/10 19:24
加载中...