ACM hash哈希字符串 模板 hdu4080

先二分长度,然后用hash来查询重复(使用了两重哈希)

718MS2284K

/*
 * Author:  nick wong
 * Created Time:  2014年08月23日 星期六 14时57分27秒
 * File Name: j.cpp
 */
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define out(x) cout<<#x<<": "<<x<<endl
const double eps(1e-8);
const int maxn=40100;
const long long maxint=-1u>>1;
const long long maxlong=maxint*maxint;
typedef long long lint;
const int mod1=100007;
const int mod2=1001117;
int h1[mod1+10],h2[mod1+10],f[maxn],g[maxn];
int m,pos,slen,a[mod1+10][2];
char s[maxn];

void init()
{
	scanf("%s",s);
}

bool check(int len)
{
	if (len==0) return true;
	memset(h1,-1,sizeof(h1));
	memset(h2,-1,sizeof(h2));
	memset(a,0,sizeof(a));
	int x=0,y=0,p1;
	for (int i=0; i<len; i++)
	{
		x=(x*26+s[i])%mod1;
		y=(y*26+s[i])%mod2;
	}
	p1=x;
	while( !((h1[p1]==x && h2[p1]==y) || (h1[p1]==-1)) )
	{
		p1=(p1+1)%mod1;
	}
	a[p1][0]=0;
	if (h1[p1]==-1) a[p1][1]=1; else a[p1][1]++;
	h1[p1]=x;
	h2[p1]=y;

	for (int i=len; i<slen; i++)
	{
		x=(((x-s[i-len]*f[len])*26+s[i])%mod1+mod1)%mod1;
		y=(((y-s[i-len]*g[len])*26+s[i])%mod2+mod2)%mod2;
		p1=x;
		while( !((h1[p1]==x && h2[p1]==y) || (h1[p1]==-1)) )
		{
			p1=(p1+1)%mod1;
		}
		a[p1][0]=i-len+1;
		if (h1[p1]==-1) a[p1][1]=1; else a[p1][1]++;
		h1[p1]=x;
		h2[p1]=y;
	}
	bool flag=false;
	x=-1;
	for (int i=0; i<mod1; i++)
		if (h1[i]!=-1 && a[i][1]>=m)
		{
			flag=true;
			x=max(x,a[i][0]);
		}
	if (!flag) return false;
	pos=x;
	return true;
}

void work()
{
	slen=strlen(s);
	int l=0,r=slen,mid;
	for (int i=0; i<slen; i++) s[i]=s[i]-'a';
	int ans=0;
	while(l<=r)
	{
		mid=(l+r)/2;
		if (check(mid)) {l=mid+1;ans = mid; }else r=mid-1;
	}
	check(ans);
	if (ans>0) cout<<ans<<" "<<pos<<endl; else cout<<"none"<<endl;
}

int main()
{
	f[1]=g[1]=1;
	for (int i=2; i<=maxn-2; i++) 
	{
		f[i]=(f[i-1]*26)%mod1;
		g[i]=(g[i-1]*26)%mod2;
	}
	while(~scanf("%d",&m))
	{
		if (m==0) break;
		getchar();
		init();
		work();
	}
    return 0;
}

Stammering Aliens

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 718    Accepted Submission(s): 269


Problem Description
Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all efforts to decode their messages have failed so far because, as luck would have it, they have stumbled upon a race of stuttering aliens! Her team has found out that, in every long enough message, the most important words appear repeated a certain number of times as a sequence of consecutive characters, even in the middle of other words. Furthermore, sometimes they use contractions in an obscure manner. For example, if they need to say bab twice, they might just send the message babab, which has been abbreviated because the second b of the first word can be reused as the first b of the second one. 
Thus, the message contains possibly overlapping repetitions of the same words over and over again. As a result, Ellie turns to you, S.R. Hadden, for help in identifying the gist of the message. 
Given an integer m, and a string s, representing the message, your task is to find the longest substring of s that appears at least m times. For example, in the message baaaababababbababbab, the length-5 word babab is contained 3 times, namely at positions 5, 7 and 12 (where indices start at zero). No substring appearing 3 or more times is longer (see the first example from the sample input). On the other hand, no substring appears 11 times or more (see example 2). In case there are several solutions, the substring with the rightmost occurrence is preferred (see example 3).
 

Input
The input contains several test cases. Each test case consists of a line with an integer m (m >= 1), the minimum number of repetitions, followed by a line containing a string s of length between m and 40 000, inclusive. All characters in s are lowercase characters from "a" to "z". The last test case is denoted by m = 0 and must not be processed.
 

Output
Print one line of output for each test case. If there is no solution, output none; otherwise, print two integers in a line, separated by a space. The first integer denotes the maximum length of a substring appearing at least m times; the second integer gives the rightmost starting position of this substring.
 

Sample Input
  
  
3 baaaababababbababbab 11 baaaababababbababbab 3 cccccc 0
 

Sample Output
  
  
5 12 none 4 2
 

Source


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值