听说灌水区人多
  • 板块灌水区
  • 楼主laol
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/12 20:54
  • 上次更新2024/3/12 22:29:12
查看原帖
听说灌水区人多
292725
laol楼主2024/3/12 20:54

ac自动机(简单版1)

re了,0分

求调

#include<bits/stdc++.h>
using namespace std;
struct ll{int ch[30];}tre[3000005];
int num[3000005],tot,cnt[3000005];
inline void trie(string st,int k){
	int u=0;
	for(int i=0;i<st.size();i++){
		int c=st[i]-'a'+1;
		if(!tre[u].ch[c])tre[u].ch[c]=++tot;
		u=tre[u].ch[c];
	}
	num[u]++;
	//cout<<u<<" "<<num[u]<<endl;
}
int fail[3000000];
void get_fail(){
	queue<int>q;
	for(int i=1;i<=26;i++){
		int v=tre[0].ch[i];
		if(v){
			q.push(v);
			fail[v]=0;
		}
	}
	while(!q.empty()){
		int u=q.front();
	//	cout<<u<<" ";
		q.pop();
		for(int i=1;i<=26;i++){
			int v=tre[u].ch[i];
			if(v){
				fail[v]=tre[fail[u]].ch[i];
				q.push(v);
			}
			else tre[u].ch[i]=tre[fail[u]].ch[i];
		}
	}
}
int ans;
void get_ans(string st){
	for(int i=0;i<st.size();i++){
		int u=tre[u].ch[st[i]-'a'+1];
		for(int j=u;j;j=fail[j]){
			ans+=num[j];
			num[j]=0;
		}
	}
}
int n;
string t,st;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>t;
		trie(t,i);
	}
	get_fail();
	cin>>st;
	get_ans(st);
	cout<<ans;
	return 0;
} 
2024/3/12 20:54
加载中...