AC自动机求改
查看原帖
AC自动机求改
696431
lijunxi1楼主2023/11/30 13:12
#include<bits/stdc++.h>
using namespace std;
const int CT=2e7+5;
struct aczdj
{
	int tree[CT][30],c,z[CT],f[CT];
	void tj(string s)
	{
		int f=0;
		for(int i=0;i<s.size();i++)
		{
			if(tree[f][s[i]-'a']==0)c++,tree[f][s[i]-'a']=c;
			f=tree[f][s[i]-'a'];
		}
		z[f]++;
	}
	void pf()
	{
		queue<int>q;
		for(int i=0;i<26;i++)if(tree[0][i]!=0)q.push(tree[0][i]);
		while(!q.empty())
		{
			int x=q.front();
			q.pop();
			for(int i=0;i<26;i++)
			if(tree[x][i]!=0)
			{
				f[tree[x][i]]=tree[f[x]][i];
				q.push(tree[x][i]);
			}
			else tree[x][i] = tree[f[x]][i];
		}
	}
	int q(string s) {
	    int ans = 0, d = 0;
	    for (int i = 0; i < s.size(); i++) {
	        d = tree[d][s[i] - 'a'];
	        for (int j = d; j && z[j] != -1; j = f[j]) {
	            ans += z[j], z[j] = -1;
	        }
	    }
	    return ans;
	}
};
aczdj ac;
int n;
string s;
int main ()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>s;
		ac.tj(s);
	}
	cin>>s;
	cout<<ac.q(s);
}

原题

2023/11/30 13:12
加载中...