首页 > ACM题库 > HDU-杭电 > HDU 4080-Stammering Aliens-字符串-[解题报告]HOJ
2015
04-16

HDU 4080-Stammering Aliens-字符串-[解题报告]HOJ

Stammering Aliens

问题描述 :

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).

输入:

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.

输出:

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.

样例输入:

3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0

样例输出:

5 12
none
4 2

/*
其实这题才是第一次做 

但是一直不过,也不知道怎么回事儿


就去调了那个简单题做

现在才发现少了main里面的if

还有ok2 是我之前写的,但是少了个{}

题意是,在所给的字符串中找一个子串,最少出现m次,求那个最长的串,并输出最后一次的开始位置

按长度二分,判断是否符合条件即可
*/


#include<stdio.h>
#include<string.h>
const int maxn=500010;


int wa[maxn],wb[maxn],wv[maxn],ws[maxn];
 int sa[maxn],ss[maxn];
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)//r[0  n-1]   sa[0    n-1]   最大值小于m
{
    int i,j,p,*x=wa,*y=wb,*t;

    for(i=0; i<m; i++)
        ws[i]=0;
    for(i=0; i<n; i++)
        ws[x[i]=r[i]]++;//x==rank   难道是统计字符个数
    for(i=1; i<m; i++)
        ws[i]+=ws[i-1];//加上前一个
    for(i=n-1; i>=0; i--)
        sa[--ws[x[i]]]=i;//排第[i]的是谁

    for(j=1,p=1; p<n; j*=2,m=p)
    {
        for(p=0,i=n-j; i<n; i++) //变量j是当前字符串的长度,
            y[p++]=i;// 数组y保存的是对第二关键字排序的结果
        for(i=0; i<n; i++)
            if(sa[i]>=j)
                y[p++]=sa[i]-j;

        for(i=0; i<n; i++)
            wv[i]=x[y[i]];
        for(i=0; i<m; i++)
            ws[i]=0;
        for(i=0; i<n; i++)
            ws[wv[i]]++;
        for(i=1; i<m; i++)
            ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--)
            sa[--ws[wv[i]]]=y[i];

        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
    return;
}

int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++)
        rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++)
            ;
    return;
}

int pos;
int po;
int ok2(int chang,int ci,int len)
{
    po=-1;
    int zui=-1,f=0;
    int i,jishu=0;
    for(i=2;i<=len;++i)
    {
        if(height[i]>=chang)
        {
            jishu++;
            if(sa[i-1]>=zui) zui=sa[i-1];
            if(sa[i]>zui) zui=sa[i];
            if(jishu>=ci){ f=1;//少了这个括号
            if(zui>po) po=zui;}
        }else
        {
            jishu=0;
            zui=-1;
        }
    }
    if(f)
    {
        return 1;
    }else{
        return 0;
    }
}

bool ok(int x, int m,int n) {
    int i, flag = 0;
    po = -1;
    int tm = 0, tt = -1;
    for (i=2; i<=n; i++) {
        if (height[i] >= x) {
            tm++;
            if (sa[i-1] > tt) tt = sa[i-1];
            if (sa[i] > tt) tt = sa[i];
            if (tm >= m) {
                flag = 1;
            if (tt > po)
                po = tt;
        }
        }
        else {
            tm = 0;
            tt = -1;
        }
    }
    if (flag)
    return true;
    return false;
}
int  main()
{
    int m;
    while(scanf("%d",&m),m)
    {
        getchar();
        char r[maxn];

        gets(r);
        int i;
        int len=strlen(r);
		  if (m == 1) {//少了这个
            printf("%d %d\n", len, 0);
            continue;
        }
        for(i=0; i<len; ++i)
        {
            ss[i]=r[i];
        }
        ss[len]=0;
        da(ss,sa,len+1,128);

        calheight(ss,sa,len);//为什么是len???

        int ret =0,hi=len-m+1,low=1,mid,pp;
        po=0;
		pos=0;
        while(hi>=low)
        {
            mid=(hi+low)/2;
            if(ok2(mid,m-1,len))//
            {
                pp=po;
                ret=mid;
                low=mid+1;
            }else
            {
                hi=mid-1;
            }
        }
        if(ret)
        {
            printf("%d %d\n",ret,pp);
        }else
        {
            printf("none\n");
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/qq172108805/article/details/8897297


  1. 这道题目的核心一句话是:取还是不取。
    如果当前取,则index+1作为参数。如果当前不取,则任用index作为参数。

  2. 在方法1里面:

    //遍历所有的边,计算入度
    for(int i=0; i<V; i++)
    {
    degree = 0;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?

  3. 一开始就规定不相邻节点颜色相同,可能得不到最优解。我想个类似的算法,也不确定是否总能得到最优解:先着一个点,随机挑一个相邻点,着第二色,继续随机选一个点,但必须至少有一个边和已着点相邻,着上不同色,当然尽量不增加新色,直到完成。我还找不到反例验证他的错误。。希望LZ也帮想想, 有想法欢迎来邮件。谢谢