HDU 4080 Stammering Aliens

Stammering Aliens

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

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
2009 South Western European Regional Contest

题目分析:
显然需要用的字符串Hash,也比较容易想到二分答案,二分子串的最大长度,但是这个题有个十分恶心的地方,一直卡我从下午到晚上,就是unsigned long long 卡我 unsigned,不加unsigned会TLE,我吐了(本来还以为卡我map pair啥的,看了吴老师写的代码,发现思路、甚至代码都极其相似,并且我发现他的代码常数似乎比我的更大,而且也有map而且他并没有TLE,甚至我一度怀疑是他的倒序Hash起了作用,直到有一次,我瞥见了unsigned,觉得unsigned势必会减小常数,果然AC )
十一点半了,终于又改出来一个题,心安理得! 睡觉!

//出现次数不小m次的子串   并且要求该子串尽量长
//二分答案  二分长度 + 检验
//
#include <iostream>
#include <map>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std;

const int Pod = 7;
#define LL unsigned long long
#define Maxn 40100
const LL Mod = 1e9 + 7;
LL xp[Maxn],Has[Maxn];
char s[Maxn];
map<LL,int> ma;
int slen,m;

int Check(int len) {
	ma.clear();
    int pos = -1;
    int l = 0,r = len - 1;
    for(; r<slen; r++,l++) {
        LL idenity = ((Has[r] - Has[l - 1] * xp[len]) );
        //if(len == 5) printf("l = %d r = %d %lld\n",l,r,idenity);
        ma[idenity]++;
        if(ma[idenity] >= m) pos = l;
    }
    return pos;
}

int main(int argc,char* argv[]) {
    xp[0] = 1;
    for(int i=1; i<=Maxn - 1; i++) xp[i] = (xp[i - 1] * Pod) ;
    while(scanf("%d",&m) ==1 && m) {
        ma.clear();
        int Ans = -1;
        scanf("%s",s);
        slen = strlen(s);
        Has[0] = s[0] - 'a' + 1;
        for(int i=1; i<slen; i++) {
            Has[i] = (Has[i - 1] * Pod + (s[i] - 'a' + 1));
        }
        int l = 1,r = slen - m + 1,Mid;
        Ans = Check(1);
        if(Ans < 0) {
        	cout << "none" <<endl;
        	continue;
		}
        while(l <= r) {
            Mid = l + r >> 1;
            int ret = Check(Mid);// 二分长度
            if(ret < 0) { r = Mid - 1; }
            else { Ans = ret; l = Mid + 1; }
        }
        printf("%d %d\n",r,Ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35776409/article/details/107501485