用hash挫字符串。。。。
Stammering Aliens
Time Limit: 10000/5000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 576Accepted Submission(s): 213
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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long int uLL;
const int x=123;
int l,len,pos,rank[44000];
uLL hash[44000],xp[44000],haha[44000];
char str[44000];
bool cmp(int a,int b)
{
return (haha[a]!=haha[b])?(haha[a]<haha[b]):(a<b);
}
bool solve(int mid)
{
int c=1;pos=-1;
for(int i=0;i<len-mid+1;i++)
{
rank[i]=i;
haha[i]=hash[i]-hash[i+mid]*xp[mid];
}
sort(rank,rank+len-mid+1,cmp);
for(int i=0;i<len-mid+1;i++)
{
if(i==0||haha[rank[i]]!=haha[rank[i-1]])
{
c=1;
}
else
{
c++;
if(c>=l)
{
pos=max(pos,rank[i]);
}
}
}
return pos>=0;
}
int main()
{
while(scanf("%d",&l)!=EOF&&l)
{
scanf("%s",str);
len=strlen(str);
if(l==1)
{
printf("%d %d\n",len,0);
continue;
}
memset(hash,0,sizeof(hash));
memset(xp,0,sizeof(xp));xp[0]=1;
for(int i=len-1;i>=0;i--)
hash[i]=hash[i+1]*x+str[i]-'a';
for(int i=1;i<len;i++)
xp[i]=xp[i-1]*x;
if(!solve(1))
{
puts("none"); continue;
}
int low=1,high=len,mid,ans,aas;
while(low+1<high)
{
mid=(low+high)>>1;
if(solve(mid)) low=mid;
else high=mid;
}
solve(low);
printf("%d %d\n",low,pos);
}
return 0;
}
分享到:
相关推荐
HDOJ题目分类HDOJ题目分类HDOJ题目分类
hdoj1001标程
ACM ICPC HDOJ1002
ACM ICPC HDOJ1001
hdoj上的资源,代码有注释,很不错的哦
hdoj1004,解题代码,答案代码,欢迎下载
ACM ICPC HDOJ1003
杭州电子科技大学hdoj1002,大整数相加问题
ACM ICPC HDOJ1008
杭州电子科大HDOJ
c语言 最短路 是hdoj上的一个最短路问题,写的很牛
ACM ICPC HDOJ1000
一些HDOJ上的DP题目的小总结,但愿能帮到那些想专攻DP的人吧
hdoj解题代码,题目为1000-1050
codj,hdoj的源码(50-60题)
hdoj 2013 多校训练3标程+解题报告
HDOJ 源代码 包含几百道HDOJ题目源码
hdoj1005 Number Sequence, 杭州电子科技大学oj题目代码
杭电OJ(1000-1099) AC 代码
HDOJ使用指南——公开版.docHDOJ使用指南——公开版.docHDOJ使用指南——公开版.doc