UVA 1449 Dominating Patterns

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
 
用刘汝佳模板。。
 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<string>
using namespace std;

#define sigma_size 26       
#define maxnode 11000    //节点个数 
#define MAXS 155
map<string,int> ms;
struct Trie//Trie模板  
{  
	int f[maxnode];
	int last[maxnode];
	int cnt[MAXS];
    int ch[maxnode][sigma_size];//maxnode表示节点个数,每个节点有26种情况,所以ch数组能表示所有情况  
    int val[maxnode];           //存节点的信息  
    int sz;                     //节点总个数  
    void clear(){
		sz=1; 
		memset(ch[0],0,sizeof(ch[0]));
		memset(cnt,0,sizeof(cnt));
		ms.clear();
	}//初始化  
    int idx(char c){ return c - 'a'; }  
  
    void insert(char *s, int v)  
    {  
        int u=0,n=strlen(s);  
        for(int i=0;i<n;i++){  
            int c=idx(s[i]);  
            if(!ch[u][c]){ //节点不存在  
                memset(ch[sz],0,sizeof(ch[sz]));  
                val[sz]=0;  
                ch[u][c]=sz++;  
            }  
            u=ch[u][c];//这样存就可以保证从每个节点,都能访问他的下一个节点了  
        }  
        val[u]=v;  
		ms[string(s)]=v;
    }

	void getFail()
	{
		queue<int> q;
		f[0]=0;
		for(int c=0;c<sigma_size;c++){
			int u=ch[0][c];
			if(u){f[u]=0;  q.push(u);  last[u]=0; }
		}
		while(!q.empty()){
			int r=q.front(); q.pop();
			for(int c=0;c<sigma_size;c++){
				int u=ch[r][c];
				if(!u) {ch[r][c]=ch[f[r]][c]; continue;}
				q.push(u);
				int v=f[r];
				while(v && !ch[v][c] )  v=f[v];
				f[u]=ch[v][c];
				last[u]=val[f[u]]?f[u]:last[f[u]];
			}
		}
	}

	void find(char *t){
		int n=strlen(t);
		int j=0;
		for(int i=0;i<n;i++){
			int c=idx(t[i]);
			j=ch[j][c];
			if(val[j]) print(j);
			else if(last[j]) print(last[j]);
		}
	}

	void print(int j)
	{
		if(j){
			cnt[val[j]]++;
			print(last[j]);
		}
	}
};  


const int MAXN=1e6+10;
Trie node; 
char p[MAXS][80];
char text[MAXN];
int main()
{
	int t;
	int i,j,k;
	while(scanf("%d",&t),t)
	{
		node.clear();
		for(i=1;i<=t;i++)
		{
			scanf("%s",p[i]);
			node.insert(p[i],i);
		}
		node.getFail();
		scanf("%s",text);
		node.find(text);
		int best=-1;
		for(i=1;i<=t;i++)
			if(node.cnt[i]>best) best=node.cnt[i];
		printf("%d\n",best);
		for(i=1;i<=t;i++)
			if(node.cnt[ms[string(p[i])]]==best)
				printf("%s\n",p[i]);
	}
	return 0;
}	


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值