LA 4760 Dominating Patterns AC自动机

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
 
题意:有n个小写字母组成的字符串和一个文本串T,找出在文本中出现次数最多的字符串。
思路:在Trie中插入单词,单词的最后结点存这是第几个单词。Query的时候找到末结点就不断fail更新单词出现次数。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 15000
struct node
{
	node * fail;
	node * next[26];
	int cnt;
	node()
	{
		fail = NULL;
		for(int i = 0;i < 26;i++)
			next[i] = NULL;
		cnt = 0;
	}
}*q[maxn];
char str[158][78],T[1000080];
int first,rear;
int num[maxn];
void Creat_Trie(char * s,node * root,int k)
{
	int i,id,len;
	node * p = root;
	len = strlen(s);
	for(int i = 0;i < len;i++)
	{
		id = s[i] - 'a';
		if(p -> next[id] == NULL)
			p -> next[id] = new node();
		p = p -> next[id];
	}
	p -> cnt = k;
}

void Build_ac_automation(node * root)
{
	int i;
	root -> fail = NULL;
	q[rear++] = root;
	while(first != rear)
	{
		node * temp = q[first++];
		node * p = NULL;
		for(i = 0;i < 26;i++)
		{
			if(temp -> next[i] != NULL)
			{
				if(temp == root)
					temp -> next[i] -> fail = root;
				else 
				{
					p = temp -> fail;
					while(p != NULL)
					{
						if(p -> next[i] != NULL)
						{
							temp -> next[i] -> fail = p -> next[i];
							break;
						}
						p = p -> fail;
					}
					if(p == NULL)
						temp -> next[i] -> fail = root;
				}
				q[rear++] = temp -> next[i];
			}
		}
	}
}

void Query(node * root,char * s)
{
	node * p = root;
	int len = strlen(s);
	for(int i = 0;i < len;i++)
	{
		int id = s[i] - 'a';
		while(p -> next[id] == NULL && p != root)
			p = p -> fail;
		p = p -> next[id];
		if(p == NULL)	p = root;
		node * temp = p;
		while(temp != root && temp -> cnt != 0)
		{
			num[temp -> cnt]++;
			temp = temp -> fail;
		}
	}
}

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF && n)
	{
		memset(num,0,sizeof(num));
		first = rear = 0;
		node * root = new node();
		for(int i = 1;i <= n;i++)
		{
			scanf("%s",str[i]);
			Creat_Trie(str[i],root,i);
		}
		Build_ac_automation(root);
		scanf("%s",T);
		Query(root,T);
		int ans = 0;
		for(int i = 1;i <= n;i++)
		{
			if(num[i] > ans)	ans = num[i];
		}
		printf("%d\n",ans);
		for(int i = 1;i <= n;i++)
			if(num[i] == ans)	printf("%s\n",str[i]);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值