ZOJ3395 Stammering Aliens 二分+后缀数组

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
那天做比赛,出这道题的人很多,所以自己硬着头皮在钻,到后来才发现原来用后缀数组就可以轻松秒杀的,之前几天看了好多篇国家集训队的论文,稍微有点掌握了,至少能运用模板了!
说下题目意思吧,给点一个m和一个字符串,要求在字符串中可以重叠出现至少M次的子串,问最长是多少,且最右边的是哪个。若无则输出none;
首先用后缀数组出来,我用的是吉林大的模板,应该数据不大,套了简单的倍增模板(O(nlog(n));然后先处理特殊情况,既是否是none情况,很简单,只需要查一下长度为1的情况是否满足条件,若不满足,则是none,否则肯定存在至少长度为1的情况,然后我们很容易想到是二分枚举长度,并记录最右边的值!
有点注意的是当m=1的时候,需要特殊处理,这时候很显然直接输出strlen(s)和0;
#include<iostream>
#include<string.h>
#include<memory.h>
#include<algorithm>
using namespace std;
#define N 40010
char s[N]; // N > 256
int n, ranks[N], sa[N], height[N],tmp[N], top[N];
void makesa(){ // O(N * log N)
    int i, j, len, na;
    na = (n < 256 ? 256 : n);
    memset(top, 0, na * sizeof(int));
    for (i = 0; i < n ; i++) top[ ranks[i] = s[i] & 0xff ]++;
    for (i = 1; i < na; i++) top[i] += top[i - 1];
    for (i = 0; i < n ; i++) sa[ --top[ ranks[i] ] ] = i;
    for (len = 1; len < n; len <<= 1) {
        for (i = 0; i < n; i++) {
            j = sa[i] - len; if (j < 0) j += n;
            tmp[ top[ ranks[j] ]++ ] = j;
        }
        sa[ tmp[ top[0] = 0 ] ] = j = 0;
        for (i = 1; i < n; i++) {
            if (ranks[ tmp[i] ] != ranks[ tmp[i-1] ] ||
                ranks[ tmp[i]+len ]!=ranks[ tmp[i-1]+len ])
                top[++j] = i;
            sa[ tmp[i] ] = j;
        }
        memcpy(ranks, sa , n * sizeof(int));
        memcpy(sa , tmp, n * sizeof(int));
        if (j >= n - 1) break;
    }
}
void lcp(){ // O(4 * N)
    int i, j, k;
    for (j = ranks[height[i=k=0]=0]; i < n - 1; i++, k++)
        while (k >= 0 && s[i] != s[ sa[j-1] + k ])
            height[j] = (k--), j = ranks[ sa[j] + 1 ];
}
int times,len;
int check(int l)
{
    int i,cnt,rp=-1,temp;
    for(i=1;i<=len;i++)
        if(height[i]>=l)
        {
            cnt=1;temp=max(sa[i],sa[i-1]);
            while(i+1<=len&&height[i+1]>=l)
            {
                i++;
                cnt++;
                temp=max(temp,sa[i]);
            }
            if(cnt>=times-1)
                rp=max(rp,temp);
        }
        return rp;
}
int main()
{
    int i,j,len2;
    while(scanf("%d",×),times)
    {
        scanf("%s",s);
        n=strlen(s);
if(1==times)
{
printf("%d 0/n",n);
continue;
}
        len=n;
        s[n++]=0;
        makesa();
        lcp();
        int ans;
        int l=1,r=len,mid,maxlen,temp;
        if(check(1)==-1)
{
printf("none/n");
continue;
}
        while(l<=r)
        {
            mid=(l+r)>>1;
            if((temp=check(mid))!=-1)
            {
ans=temp;
                maxlen=mid;
                l=mid+1;
            }
            else
                r=mid-1;
        }
        printf("%d %d/n",maxlen,ans);
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值