AC自动机九连测

T 1 : T1:病毒侵袭
A C   C o d e \mathrm{AC \ Code}

#include<bits/stdc++.h>
#define maxn 100005
#define maxc 95
#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)
#define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--) 
using namespace std;

int total;
namespace AC{
	int tr[maxn][maxc],fa[maxn],tot;
	set<int>ed[maxn];
	
	void clr(){
		for(;tot;) memset(tr[tot],0,sizeof tr[tot]),ed[tot].clear(),fa[tot--]=0;
 	}
	
	void ins(char *s,int n,int id){
		int u=0,v;
		rep(i,0,n-1) (!tr[u][v=s[i]-32])&&(tr[u][v]=++tot),u=tr[u][v];
		ed[u].insert(id);
	}
	
	void Build(){
		static int q[maxn],L,R;
		rep(i,L=R=0,maxc-1) if(tr[0][i]) q[R++]=tr[0][i];
		for(int u;L<R;){
			u=q[L++];
			if(ed[fa[u]].size() <= 3)
				ed[u].insert(ed[fa[u]].begin(),ed[fa[u]].end());
			rep(i,0,maxc-1) 
				if(tr[u][i]) 
					q[R++]=tr[u][i],fa[tr[u][i]]=tr[fa[u]][i];
				else 
					tr[u][i] = tr[fa[u]][i];
		}
	}
	void qry(char *s,int n,int id){
		int u=0,v;
		set<int>r;
		rep(i,0,n-1) 
			u=tr[u][v=s[i]-32],
			r.insert(ed[u].begin(),ed[u].end());
		if(r.size()){
			total++;
			printf("web %d:",id);
			for(set<int>::iterator it=r.begin();it!=r.end();it++)
				printf(" %d",*it);
			putchar('\n');
		}
	}
}

int main(){
	int n;scanf("%d",&n);
	rep(i,1,n){
		static char s[205];
		scanf("%s",s);
		int m = strlen(s);
		AC::ins(s,m,i);
	}
	AC::Build();
	scanf("%d",&n);
	rep(i,1,n){
		static char s[10005];
		scanf("%s",s);
		int m = strlen(s);
		AC::qry(s,m,i);
	}
	printf("total: %d\n",total);
}

T 2 \mathrm{T2 考研路茫茫——单词情结 }
A C   C o d e \mathrm{AC \ Code}

#include<bits/stdc++.h>
#define maxn 27
#define maxc 26
#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)
#define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--)
#define Ct const
#define LL unsigned long long
using namespace std;

int N;
struct mat{
	LL a[maxn][maxn];
	mat(LL d=0){ memset(a,0,sizeof a);rep(i,0,maxn-1) a[i][i]=d; }
	mat operator *(Ct mat &B)Ct{
		mat r;
		rep(i,0,N-1) rep(j,0,N-1) if(a[i][j]) rep(k,0,N-1) if(B.a[j][k])
			r.a[i][k] += a[i][j] * B.a[j][k];
		return r;
	}
}A;

mat Pow(mat b,LL k){
	mat r(1);
	for(;k;k>>=1,b=b*b) 
		if(k&1)
			r=b*r;
	return r;
}

namespace AC{
	int tr[maxn][maxc],fa[maxn],sz;
	bool tag[maxn];
	void clr(){
		for(;~sz;) memset(tr[sz],0,sizeof tr[sz]),tag[sz]=0,fa[sz--]=0;
		sz=0;
	}
	
	void ins(char *s,int n){
		int u=0,v;
		rep(i,0,n-1) (!tr[u][v=s[i]-'a'])&&(tr[u][v]=++sz),u=tr[u][v];
		tag[u]=1;
	}
	
	void Build(){
		static int q[maxn],L,R;
		rep(i,L=R=0,maxc-1) if(tr[0][i]) q[R++]=tr[0][i];
		for(int u;L<R;){
			u=q[L++];
			tag[u] |= tag[fa[u]];
			rep(i,0,maxc-1) 
				if(tr[u][i]) 
					fa[tr[u][i]]=tr[fa[u]][i],q[R++]=tr[u][i];
				else
					tr[u][i] = tr[fa[u]][i];
		}
		N = sz+2;
		rep(i,0,sz) if(!tag[i])
			rep(j,0,maxc-1) if(!tag[tr[i][j]])
				A.a[i][tr[i][j]]++;
		rep(i,0,N-1) 
			A.a[i][N-1]++;
	}
}

LL cal(int L){
	LL a=1,b=26,ra=1,rb=26;
	for(;L;L>>=1,a=a+a*b,b*=b)
		if(L&1)
			ra=ra+a*rb,rb*=b;
	return ra;
}

int main(){
	int n,L;
	while(~scanf("%d%d",&n,&L)){
		AC::clr();A=mat(0);
		for(;n--;){
			static char s[6];
			scanf("%s",s);
			int m =strlen(s);
			AC::ins(s,m);
		}
		AC::Build();
		A = Pow(A , L+1ll);
		LL ans = cal(L) - A.a[0][N-1];
		printf("%llu\n",ans);
	}
}

T 3 W i r e l e s s P a s s w o r d T3Wireless Password
A C   C o d e \mathrm{AC \ Code}

#include<bits/stdc++.h>
#define maxn 255
#define maxc 26
#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)
#define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--)
#define mod 20090717
using namespace std;

int n,m,k,M,bits[1<<10];
void add(int &a,int b){ a+=b;if(a>=mod) a-=mod; }
namespace AC{
	int f[2][maxn][1<<10];
	int tr[maxn][maxc],fa[maxn],tag[maxn],sz;
	void clr(){
		for(;~sz;) memset(tr[sz],0,sizeof tr[sz]),memset(f[0][sz],0,sizeof f[0][sz]),memset(f[1][sz],0,sizeof f[1][sz])
			,fa[sz]=0,tag[sz--]=0;
		sz=0;
	}
	void ins(char *s,int n,int id){
		int u=0,v;
		rep(i,0,n-1) !tr[u][v=s[i]-'a']&&(tr[u][v]=++sz),u=tr[u][v];
		tag[u]|=1<<id;
	}
	#define ADC(i) rep(i,0,maxc-1)
	void Build(){
		static int q[maxn],L,R;L=R=0;
		ADC(i) if(tr[0][i]) q[R++]=tr[0][i];
		for(int u;L<R;){ u=q[L++];
			tag[u] |= tag[fa[u]];
			ADC(i) if(tr[u][i]) q[R++] = tr[u][i] , fa[tr[u][i]] = tr[fa[u]][i];
			else tr[u][i] = tr[fa[u]][i];
		}
	}
	void Solve(){
		int now = 1 , pre = 0;
		f[now][0][tag[0]] = 1;
		for(;n--;){
			swap(now,pre);
			rep(i,0,sz) rep(j,0,M-1) if(f[pre][i][j]){
				ADC(k) add(f[now][tr[i][k]][j|tag[tr[i][k]]],f[pre][i][j]);
				f[pre][i][j] = 0;
			}
		}
		int ans = 0;
		rep(j,0,M-1) if(bits[j]>=k) rep(i,0,sz) 
			add(ans , f[now][i][j]);
		printf("%d\n",ans);
	}
}
	
int main(){
	rep(i,1,(1<<10)-1) bits[i] = bits[i>>1] + (i&1);
	while(~scanf("%d%d%d",&n,&m,&k) && n){
		AC::clr();M=1<<m;
		rep(i,0,m-1){
			static char s[maxn];
			scanf("%s",s);
			int m = strlen(s);
			AC::ins(s,m,i);
		}
		AC::Build();
		AC::Solve();
	}
}

$T4Resource Archiver $
A C   C o d e \mathrm{AC \ Code}

#include<bits/stdc++.h>
#define maxn 60005
#define maxc 2
#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)
#define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--)
using namespace std;

int n,m,N;
bool vis[maxn][1<<10];
namespace AC{
	int tr[maxn][maxc],fa[maxn],tag[maxn],sz;
	void clr(){
		for(;~sz;) memset(tr[sz],0,sizeof tr[sz]),tag[sz]=fa[sz]=0,sz--;
		sz=0;
	}
	void ins(char *s,int n,int id){
		int u=0,v;
		rep(i,0,n-1) !tr[u][v=s[i]-'0']&&(tr[u][v]=++sz),u=tr[u][v];
		tag[u]|=1<<id;
	}
	void Build(){
		static int q[maxn],L,R;
		rep(i,L=R=0,1) if(tr[0][i]) q[R++]=tr[0][i]; 
		for(int u=0;L<R;){
			u=q[L++];
			tag[u] |= tag[fa[u]];
			rep(i,0,1) if(tr[u][i]) q[R++]=tr[u][i],fa[tr[u][i]]=tr[fa[u]][i];
			else tr[u][i]=tr[fa[u]][i];
		}
	}
	struct node{
		int d,s,p;
		node(int d=0,int s=0,int p=0):d(d),s(s),p(p){}
	};
	void BFS(){
		static queue<node>q;
		for(;!q.empty();q.pop());
		q.push(node(0,tag[0],0));
		memset(vis,0,sizeof vis);
		for(node u;!q.empty();){
			u=q.front();q.pop();
			vis[u.p][u.s] = 1;
			if(u.s == N-1){
				printf("%d\n",u.d);
				break;
			}
			int v;
			rep(i,0,1) if(tag[v=tr[u.p][i]]<N && !vis[v][u.s|tag[v]]){
				vis[v][u.s|tag[v]] = 1;
				q.push(node(u.d+1,u.s|tag[v],v));
			}
		}
	}
}

int main(){
	while(~scanf("%d%d",&n,&m)&&n){
		AC::clr();N=1<<n;
		rep(i,0,n-1){
			static char s[1005];
			scanf("%s",s);
			int m = strlen(s);
			AC::ins(s,m,i);
		}
		rep(i,0,m-1){
			static char s[maxn];
			scanf("%s",s);
			int m = strlen(s);
			AC::ins(s,m,n);
		}
		AC::Build();
		AC::BFS();
	}
}

T 5 T5小明系列故事——女友的考验

A C   C o d e \mathrm{AC \ Code}

#include<bits/stdc++.h>
#define maxn 505
#define maxc 51
#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)
#define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--)
#define db double
using namespace std;

int n,m,k;
db x[maxn],y[maxn],ds[55][55];
db sqr(db a){ return a * a; }
db dis(int u,int v){ return sqrt(sqr(x[u]-x[v])+sqr(y[u]-y[v])); }
namespace AC{
	int tr[maxn][maxc],fa[maxn],tag[maxn],sz;
	db h[maxn][55];
	void clr(){
		for(;~sz;){
			memset(tr[sz],0,sizeof tr[sz]);
			rep(i,0,50) h[sz][i]=1e18;
			fa[sz]=0,tag[sz--]=0;
		}
		sz=0;
	}
	void ins(int *s,int n){
		int u=0,v;
		rep(i,0,n-1) !tr[u][v=s[i]]&&(tr[u][v]=++sz),u=tr[u][v];
		tag[u]=1;
	}
	void Build(){
		static int q[maxn],L,R;
		rep(i,L=R=1,n) if(tr[0][i]) q[R++]=tr[0][i];
		for(int u;L<R;){
			u=q[L++];
			tag[u]|=tag[fa[u]];
			rep(i,1,n) if(tr[u][i]) q[R++]=tr[u][i],fa[tr[u][i]]=tr[fa[u]][i];
			else tr[u][i]=tr[fa[u]][i];
		}
	}
	
	#define pii pair<int,int>
	#define mp make_pair
	void Solve(){
		queue<pii >q;
		q.push(mp(1,tr[0][1]));
		h[tr[0][1]][1] = 0;
		static bool inq[maxn][55]={};
		memset(inq,0,sizeof inq);
		for(int u,p,v;!q.empty();){
			u=q.front().first,p=q.front().second,q.pop();
			rep(i,u+1,n) if(!tag[v=tr[p][i]] && h[v][i]>h[p][u]+ds[u][i]){
				h[v][i] = h[p][u] + ds[u][i];
				if(!inq[v][i]) inq[v][i]=1,q.push(mp(i,v));
			}
			inq[p][u] = 0;
		}
		db ans = 1e18;
		rep(i,0,sz) ans = min(ans , h[i][n]);
		if(ans > 1e17) puts("Can not be reached!");
		else printf("%.2lf\n",ans);
	}
}
int main(){
	AC::sz=maxn-1;
	while(~scanf("%d%d",&n,&m)&&n){
		AC::clr();
		rep(i,1,n) scanf("%lf%lf",&x[i],&y[i]);
		rep(i,1,n) rep(j,1,n) ds[i][j] = dis(i,j);
		for(;m--;){
			static int s[maxn];
			int N;scanf("%d",&N);
			rep(i,0,N-1) scanf("%d",&s[i]);
			AC::ins(s,N);
		}
		AC::Build();
		AC::Solve();
	}
}

T 6 L o s t s r e v e n g e H D U 3341 T6 Lost's revenge HDU - 3341
A C   C o d e \mathrm{AC \ Code}

#include<bits/stdc++.h>
#define maxn 505
#define maxc 4
#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)
#define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--)
using namespace std;

int mAp[300];

void chk(int &a,int b){ a = max(a , b); }

namespace AC{
	int tr[maxn][maxc],fa[maxn],tag[maxn],sz;
	void clr(){
		for(;~sz;) memset(tr[sz],0,sizeof tr[sz]),fa[sz]=0,tag[sz--]=0;
		sz=0;
	}
	void ins(char *s,int n){
		int u=0,v;
		rep(i,0,n-1) !tr[u][v=mAp[s[i]]]&&(tr[u][v]=++sz),u=tr[u][v];
		tag[u]++;
	}
	void Build(){
		static int q[maxn],L,R;
		rep(i,L=R=0,maxc-1) if(tr[0][i]) q[R++]=tr[0][i];
		for(int u;L<R;){
			u=q[L++];
			tag[u] += tag[fa[u]];
			rep(i,0,maxc-1) if(tr[u][i]) q[R++]=tr[u][i] , fa[tr[u][i]] = tr[fa[u]][i];
			else tr[u][i] = tr[fa[u]][i];
		}
	}
	#define vc vector
	void qry(){
		static char s[maxn];
		static int T = 0;
		int c[4] = {};
		scanf("%s",s);int m = strlen(s);
		rep(i,0,m-1) c[mAp[s[i]]]++;
		vc<vc<vc<vc<vc<int>>>>>f(c[0]+1,
			vc<vc<vc<vc<int>>>>(c[1]+1,
			vc<vc<vc<int>>>(c[2]+1,
			vc<vc<int>>(c[3]+1,
			vc<int>(sz+1)))));
				
		rep(a0,0,c[0]) rep(a1,0,c[1]) rep(a2,0,c[2]) rep(a3,0,c[3]) rep(p,0,sz)
			f[a0][a1][a2][a3][p] = -0x3f3f3f3f;
		f[0][0][0][0][0] = 0;
		rep(a0,0,c[0]) rep(a1,0,c[1]) rep(a2,0,c[2]) rep(a3,0,c[3]) rep(p,0,sz)
			if(f[a0][a1][a2][a3][p]>=0){
				int a[4] = {a0,a1,a2,a3} , t = f[a0][a1][a2][a3][p];
				rep(k,0,3) if(a[k]+1<=c[k]){
					int v = tr[p][k];
					a[k]++;
					chk(f[a[0]][a[1]][a[2]][a[3]][v] , t + tag[v]);
					a[k]--;
				}
			}
			
		int ans = 0;
		rep(p,0,sz) ans = max(ans , f[c[0]][c[1]][c[2]][c[3]][p]);
		printf("Case %d: %d\n",++T,ans);
	}
}

int main(){
	mAp['A']=0,mAp['C']=1,mAp['G']=2,mAp['T']=3;
	int n;
	for(;~scanf("%d",&n) && n;){
		AC::clr();
		rep(i,1,n){
			static char s[11];
			scanf("%s",s);int m = strlen(s);
			AC::ins(s,m);
		}
		AC::Build();
		AC::qry();
	}
}

T 7 D N A S e q u e n c e T7DNA Sequence
A C   C o d e \mathrm{AC \ Code}

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 505
#define maxc 4
#define mod 100000
#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)
#define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--)
#define Ct const
using namespace std;

int mAp[300];
int N;

struct mat{
	int a[101][101];
	mat(int d=0){ memset(a,0,sizeof a);rep(i,0,N-1) a[i][i] = d; }
	mat operator *(Ct mat &B)Ct{
		mat r(0);
		rep(i,0,N-1) rep(j,0,N-1) if(a[i][j]) rep(k,0,N-1) if(B.a[j][k])
			r.a[i][k] = (r.a[i][k] + (1ll * a[i][j] * B.a[j][k])) % mod;
		return r;
	}
}A;

mat Pow(mat b,int k){
	mat r(1);
	for(;k;k>>=1,b=b*b) if(k&1) r=r*b;
	return r;
}

namespace AC{
	int tr[maxn][maxc],fa[maxn],tag[maxn],sz;
	void clr(){
		for(;~sz;) memset(tr[sz],0,sizeof tr[sz]),fa[sz]=0,tag[sz--]=0;
		sz=0;
	}
	void ins(char *s,int n){
		int u=0,v;
		rep(i,0,n-1) !tr[u][v=mAp[s[i]]]&&(tr[u][v]=++sz),u=tr[u][v];
		tag[u]=1;
	}
	void Build(){
		static int q[maxn],L,R;
		rep(i,L=R=0,maxc-1) if(tr[0][i]) q[R++]=tr[0][i];
		for(int u;L<R;){
			u=q[L++];
			tag[u] |= tag[fa[u]];
			rep(i,0,maxc-1) if(tr[u][i]) q[R++]=tr[u][i] , fa[tr[u][i]] = tr[fa[u]][i];
			else tr[u][i] = tr[fa[u]][i];
		}
		N = sz+1;
		rep(i,0,sz) if(!tag[i]) rep(j,0,maxc-1) if(!tag[tr[i][j]])
			A.a[i][tr[i][j]]++; 
	}
}

int main(){
	mAp['A']=0,mAp['C']=1,mAp['G']=2,mAp['T']=3;
	int m,n;
	scanf("%d%d",&m,&n);
	rep(i,1,m){
		static char s[11];
		scanf("%s",s);int m = strlen(s);
		AC::ins(s,m);
	}
	AC::Build();
	A = Pow(A,n);
	int ans = 0;
	rep(i,0,N-1)
		ans = (ans + A.a[0][i]) % mod;
	printf("%d\n",ans);
}

T 8 C e n s o r e d ! T8Censored!
A C   C o d e \mathrm{AC \ Code}

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define maxn 105
#define LL long long
#define mod 10000000000000000ll
using namespace std;

int n,m;
int F[maxn*5],tr[maxn][55],fail[maxn],tag[maxn],tot;
char s[105];
inline void Insert(char *s,int u=0)
{	for(int i=0,len=strlen(s),v;i<len;i++) v=F[s[i]],u=tr[u][v]?tr[u][v]:tr[u][v]=++tot;
	tag[u]=1;}
void Build()
{	queue<int>q;
	for(int i=0;i<n;i++) if(tr[0][i]) q.push(tr[0][i]),fail[tr[0][i]]=0;
	for(int u;!q.empty();)
	{	u=q.front(),q.pop();
		for(int i=0;i<n;i++) 
			if(tr[u][i]) fail[tr[u][i]] = tr[fail[u]][i],q.push(tr[u][i]),tag[tr[u][i]]|=tag[fail[tr[u][i]]];
			else tr[u][i] = tr[fail[u]][i];
	}
}
LL dp[2][maxn][11];
inline void addto(LL *a,LL *b)
{
	for(int i=0;i<10;i++)
	{
		a[i]+=b[i];
		if(a[i] >= mod) a[i]-=mod, a[i+1]++; 
	}
}
int main()
{
	int P;
	scanf("%d%d%d",&n,&m,&P);
	scanf("%s",s);
	for(int i=0;i<n;i++) F[s[i]]=i;
	for(;P--;) scanf("%s",s),Insert(s);
	Build();
	
	int now = 1, pre = 0;
	memset(dp,0,sizeof dp);
	dp[pre][0][0] = 1;
	for(;m--;swap(now,pre))
	{
		memset(dp[now],0,sizeof dp[now]);
		for(int i=0;i<=tot;i++)
			if(!tag[i])	for(int j=0;j<n;j++)
				if(!tag[tr[i][j]]) 
					addto(dp[now][tr[i][j]],dp[pre][i]);
	}
	
	LL ans[11]={};
	for(int i=0;i<=tot;i++)
		if(!tag[i])
			addto(ans,dp[pre][i]);
	for(int i=10;i>=0;i--)
		if(ans[i])
		{
			printf("%lld",ans[i]);
			for(i--;i>=0;i--) printf("%016lld",ans[i]);
		}
		else if(i == 0) printf("0");
}

T 9 K e y w o r d s S e a r c h T9 Keywords Search
A C   C o d e \mathrm{AC \ Code}

#include<bits/stdc++.h>
#define maxn 500005
#define maxc 26
#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)
#define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--)
using namespace std;

namespace AC{
	int tr[maxn][maxc],fa[maxn],tag[maxn],sz;
	void clr(){
		for(;~sz;) memset(tr[sz],0,sizeof tr[sz]),fa[sz]=0,tag[sz--]=0;
		sz=0;
	}
	void ins(char *s,int n){
		int u=0,v;
		rep(i,0,n-1) !tr[u][v=s[i]-'a']&&(tr[u][v]=++sz),u=tr[u][v];
		tag[u]++;
	}
	void Build(){
		static int q[maxn],L,R;
		rep(i,L=R=0,maxc-1) if(tr[0][i]) q[R++]=tr[0][i];
		for(int u;L<R;){
			u=q[L++];
			rep(i,0,maxc-1) if(tr[u][i]) q[R++]=tr[u][i],fa[tr[u][i]]=tr[fa[u]][i];
			else tr[u][i] = tr[fa[u]][i];
		}
	}
	void Solve(){
		static char s[maxn*2];
		scanf("%s",s);
		int m = strlen(s),v,u=0;
		int ans = 0;
		rep(i,0,m-1){
			u=tr[u][v=s[i]-'a'];
			for(int j=u;j;j=fa[j]){
				if(tag[j] == -1) break;
				ans += tag[j];
				tag[j] = -1;
			}
		}
		printf("%d\n",ans);
	}
}

int main(){
	int T;
	scanf("%d",&T);
	for(;T--;){
		AC::clr();
		int n;scanf("%d",&n);
		rep(i,1,n){
			static char s[maxn];
			scanf("%s",s);
			int m = strlen(s);
			AC::ins(s,m);
		}
		AC::Build();
		AC::Solve();
	}
}
发布了640 篇原创文章 · 获赞 97 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_35950004/article/details/104055635
今日推荐