ZOJ-3395 Stammering Aliens(二分+字符串hash)

ZOJ Problem Set - 3395

Stammering Aliens


Time Limit: 5 Seconds      Memory Limit: 65536 KB


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: Southwestern Europe Regional Contest 2009

 

一、原题地址

点我传送

 

二、大致题意

每组给出一个K ,和一个字符串,要求找出这串字符中出现次数大于K次的最长子串长度,以及最后一个最长子串所在的起始位置(头位置为 0 )。

 

三、大致思路

二分子串的长度,然后O(n)的check是否存在K个以上的串。

O(n)的check里一开始是用string处理子串,但是MLE了,所以采用字符串hash。

大家都用的自动机,但是我不会呀QAQ

 

四、代码

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<stack>
#include<bitset>
using namespace std;
#define PI 3.14159265
const int inf=0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ull;


const ull ThatMan=19260817;
ull mul[40005],pre[40005];
char s[40005];
int n,len,maxx,anspos;
map<ull,int>vis;

void init()
{
    mul[0]=1;
    for(int i=1;i<=40000;i++) mul[i]=mul[i-1]*ThatMan;
}

void HashString()
{
    pre[0]=0;
    for(int i=1;i<=len;i++) pre[i]=pre[i-1]*ThatMan+(s[i]-'a'+1);
}

bool check(int mid)
{
    vis.clear();
    bool tag=false;
    for(int i=1;i+mid-1<=len;i++)
    {
        ull righ=pre[i+mid-1];
        ull left=pre[i-1]*mul[mid];
        ull HashNum=righ-left;
        vis[HashNum]++;
        if(vis[HashNum]>=n)
        {
            if(mid>=maxx)
            {
                maxx=mid;anspos=i;
            }
            tag=true;
        }
    }
    return tag;
}

int main()
{
    init();
    while(scanf("%d",&n),n)
    {
        scanf("%s",s+1);
        len=strlen(s+1);
        HashString();

        int l=1,r=len;
        int mid;
        maxx=anspos=-1;
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(check(mid))
            {
                l=mid+1;
            }
            else r=mid-1;
        }
        if(maxx!=-1)printf("%d %d\n",maxx,anspos-1);
        else printf("none\n");
    }
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值