RE50pts求助
查看原帖
RE50pts求助
800499
suzhikz楼主2024/7/10 13:15
#include<bits/stdc++.h>
using namespace std;
struct node{
	int cnt;
	int ch[30];
	int fail;
}trie[5000005];
int n,cnt;
char c[1000005];
void work(){
	queue<int>q;
	for(int i=0;i<26;i++){
		if(trie[0].ch[i]){
			trie[trie[0].ch[i]].fail=0;
			q.push(trie[0].ch[i]);
		}
	}
	while(!q.empty()){
		int u=q.front();
		q.pop();
		int f=trie[u].fail;
		for(int i=0;i<26;i++){
			int v=trie[u].ch[i];
			if(v){
				trie[v].fail=trie[f].ch[i];
				q.push(v);
			}else{
				trie[u].ch[i]=trie[f].ch[i];
			}
		}
	}
}
int ans;
void AC(){
	int u=0;
	int m=strlen(c+1);
	for(int i=1;i<=m;i++){
		int v=c[i]-'0';
		int c=trie[u].ch[v];
		while(c&&trie[c].cnt!=-1){
			ans+=trie[c].cnt;
			trie[c].cnt=-1;
			c=trie[c].fail;
		}
		u=trie[u].ch[v];
	}
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%s",c+1);
		int m=strlen(c+1);
		int u=0;
		for(int j=1;j<=m;j++){
			if(!trie[u].ch[c[j]-'0']){
				trie[u].ch[c[j]-'0']=++cnt;
			}
			u=trie[u].ch[c[j]-'0'];
		}
		trie[u].cnt++;
	}
	work();
	scanf("%s",c+1);
	AC();
	cout<<ans;
	return 0;
}

2024/7/10 13:15
加载中...