WA自动机求调
查看原帖
WA自动机求调
813622
Igallta楼主2024/4/27 11:06

WA #2

他们说没有建 fail 会导致 WA#2 但我觉得我建了啊

/****************************************
作者:
版权:
日期:
*****************************************/
#include<bits/stdc++.h>
#define LL k<<1
#define RR k<<1|1
#define int long long

using namespace std;
const int N=1e6+1;
int n,cnt,ans;
queue<int>q;
string s;
struct TRIE{
	int ch[28],bj,fail;
}t[4*N];
void insert(string s){
	int root=0;
	for(auto v:s){
		int y=v-'a';
		if(!t[root].ch[y]){
			t[root].ch[y]=++cnt;
		}
		root=t[root].ch[y];
	}
	++t[root].bj;
}
void bfs(){
	int x,y;
	for(int i=0;i<26;i++){
		if(t[0].ch[i]){
			q.push(t[0].ch[i]);
		}
	}
	while(q.size()){
		int x=q.front();
		q.pop();
		for(int i=0;i<26;i++){
			if(t[x].ch[i]){
				t[t[x].ch[i]].fail=t[t[x].fail].ch[i];
				q.push(t[x].ch[i]);
			}
			else t[x].ch[i]=t[t[x].fail].ch[i];	
		}
	}
}
void match(string s){
	int i,j,p=0;
	for(auto v:s){
		p=t[p].ch[v-'a'];
		for(j=p;j&&t[j].bj!=-1;j=t[j].fail){
			ans+=t[j].bj;
			t[j].bj=-1;
		}
	}
}
signed main(){
//	freopen("AC.in","r",stdin);
//	freopen("WA.out","w",stdout);
	ios::sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s;
		insert(s);
	}
	cin>>s;
	match(s);
	cout<<ans;
	return 0;
}
2024/4/27 11:06
加载中...