UVA 1449(Dominating Patterns-第一题AC自动机)

本文深入探讨了AC自动机在解析未知语言模式中的应用与实现过程,通过实例展示了如何利用AC自动机找出文本中最显著的语言模式及其出现次数。详细介绍了AC自动机的构建、工作原理及在文本匹配中的优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string, these patterns may appear more than one times in a large text string (also only lower case English letters).

What matters most is that which patterns are the dominating patterns. Dominating pattern is the pattern whose appearing times is not less than other patterns.

It is your job to find the dominating pattern(s) and their appearing times.

INPUT

The entire input contains multi cases. The first line of each case is an integer, which is the number of patterns N1$ \le$N$ \le$150. Each of the following N lines contains one pattern, whose length is in range [1, 70]. The rest of the case is one line contains a large string as the text to lookup, whose length is up to106.

At the end of the input file, number `0' indicates the end of input file.

Output

For each of the input cases, output the appearing times of the dominating pattern(s). If there are more than one dominating pattern, output them in separate lines; and keep their input order to the output.

Sample Input

2 
aba 
bab 
ababababac 
6 
beta 
alpha 
haha 
delta 
dede 
tata 
dedeltalphahahahototatalpha 
0

Sample Output

4 
aba 
2 
alpha 
haha



裸AC自动机

按照惯例我要做一份很详细的解析放在这里……可是又麻烦……(好吧我承认我蒟蒻不会做……)

所以大家请在下面这份代码中凑合看吧.(这是什么坑爹丑代码……)

顺带说一句AC自动机fail指针必须用bfs,因为之前的结点可能指向后面的指针


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define MAXN (150+10)
#define MAXp (70+10)
#define MAXT (1000000+10)
struct node
{
	node *ch[26],*last,*f;
	int val,findtime,no;
	char c;
	node():val(0),findtime(0){Rep(i,26) ch[i]=NULL;last=NULL;f=NULL;}
	node(node *_f,int j):val(0),findtime(0){Rep(i,26) ch[i]=NULL;last=NULL;f=_f;c=j+'a';}
}*root=new node();
void getfail(node *&x,int j)
{
	node* p=x->f;
	if (p==root) {x->last=root;return;}
	for(p=p->last;p!=root&&!p->ch[j];p=p->last);
	if (!p->ch[j]) x->last=root;
	else x->last=p->ch[j];
}
void insert(char* s,int no)
{
	int len=strlen(s);
	node *now=root;
	Rep(i,len)
	{
		int j=s[i]-'a';
		if (!now->ch[j]) now->ch[j]=new node(now,j);//getfail(now->ch[j],j);
		now=now->ch[j];
	}
	now->val++;
	if (now->val==1) now->no=no;
}
node *q[MAXN*MAXp*8];
void bfs()
{
	q[1]=root;
	int head=1,tail=1;
	while (head<=tail)
	{
		node *now=q[head];
		Rep(j,26) if (now->ch[j]) q[++tail]=now->ch[j],getfail(q[tail],j);
		head++;
	}
}
int ans=0;
void increase(node *x)
{
	while (x)
	{
		if (x->val) 
		{	
			x->findtime++;
			ans=max(ans,x->findtime);
		}
		x=x->last;
	}
}
void find(char *T)
{
	int len=strlen(T);
	node *now=root;
	Rep(i,len)
	{
		int j=T[i]-'a';
		while (now!=root&&!now->ch[j]) now=now->last;
		if (now->ch[j]) now=now->ch[j],increase(now);
	}
}
int n,size=-1;
bool b[MAXN]={0};
char st[MAXp];
void print(node *x)
{
	if (x->val&&x->findtime==ans) b[x->no]=1;//st[size+1]=0,printf("%s\n",st);
	Rep(j,26) if (x->ch[j]) st[++size]=j+'a',print(x->ch[j]),size--;
}
char s[MAXN][MAXp],T[MAXT];
void delet(node *x)
{
	Rep(j,26) if (x->ch[j]) delet(x->ch[j]);
	delete x;
}
int main()
{
//	freopen("la4670.in","r",stdin);
//	freopen(".out","w",stdout);
	while (scanf("%d",&n)&&n)
	{
		memset(b,0,sizeof(b));
		ans=0;
		root=new node();
		For(i,n) scanf("%s",s[i]),insert(s[i],i);
		bfs();
		scanf("%s",T);
		find(T);
		printf("%d\n",ans);
		print(root);
		For(i,n) if (b[i]) printf("%s\n",s[i]);
		delet(root);
	//	cout<<size<<endl;
	}
	return 0;
}






                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值