`
20386053
  • 浏览: 461434 次
文章分类
社区版块
存档分类
最新评论

HDOJ 4080 Stammering Aliens

 
阅读更多

用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
5 12 none 4 2

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题目分类 HDOJ题目分类” HDOJ,全称为Happy DingO Online Judge,是一个在线编程竞赛平台,它为参赛者提供了大量编程题目进行练习和比赛,旨在提升编程技能和算法理解。HDOJ的题目分类是帮助用户...

    hdoj.rar_Dividing HDOJ_OJ 1082_hdoj 10_杭电oj_杭电oj1000

    【标题】"hdoj.rar_Dividing HDOJ_OJ 1082_hdoj 10_杭电oj_杭电oj1000" 涉及的知识点主要围绕着“杭电在线判题系统(HDOJ)”以及其中的题目1082和10系列题目。HDOJ是杭州电子科技大学主办的一个在线编程竞赛平台,...

    hdoj1001标程

    hdoj1001标程

    HDOJ1002

    ACM ICPC HDOJ1002

    HDOJ1001

    ACM ICPC HDOJ1001

    HDOJ 80题 Java

    【标题】"HDOJ 80题 Java"是一份专为Java程序员设计的在线编程挑战集合,源自杭州电子科技大学(HDOJ)的在线评测系统。这些题目旨在帮助Java开发者提升算法理解与编程能力,同时也为那些习惯于C++但希望在Java环境...

    hdoj杭电1000-2000部分解题报告

    “hdoj杭电1000-2000部分解题报告”这个标题指的是一个关于杭州电子科技大学(简称杭电)在线编程竞赛平台(HDU Online Judge,简称HDOJ)上的题目解题报告。这份报告涵盖了编号从1000到2000的题目,这是一段相当大...

    hdoj1004 解题代码 答案

    hdoj1004,解题代码,答案代码,欢迎下载

    HDOJ离线版

    《HDOJ离线版:探索编程竞赛的智慧宝库》 HDOJ,全称为“杭州电子科技大学在线评测系统”(Hangzhou Dianzi University Online Judge),是中国早期的编程竞赛平台之一,深受广大编程爱好者和在校学生的喜爱。HDOJ...

    HDOJ1003

    ACM ICPC HDOJ1003

    HDOJ 1008

    ACM ICPC HDOJ1008

    hdoj--acm题目,有注释

    "hdoj--acm题目,有注释" 本资源提供了多个 ACM 题目的解决方案,代码都带有注释,非常适合初学者学习。下面是对每个题目的知识点总结: 2000:本题目要求输入三个字符,输出按照从小到大排序的结果。本代码使用了...

    HDOJ.rar_HD_HDOJ

    【标题】"HDOJ.rar_HD_HDOJ" 是一个与HDU(杭州电子科技大学)在线判题系统HDOJ相关的压缩包文件,其中包含了大量编程题目的源代码。 【描述】提到,这个压缩包包含了几百道HDOJ题目的源代码,这意味着它是一个宝贵...

    OJ.tar.gz_HDOJ _OJ源码_oj

    【OJ.tar.gz_HDOJ _OJ源码_oj】是一个包含编程竞赛平台HDOJ(Happy Ding Octopus Judge)部分源代码的压缩文件。这个压缩包的主要目的是供学习和研究使用,尤其是针对50至60题目的解题算法和系统实现。通过分析这些...

    hdoj2066最短路

    根据给定的文件信息,我们可以总结出以下关于“hdoj2066最短路径”的相关知识点: ## hdoj2066最短路径概述 ### 标题解析:“hdoj2066最短路” - **hdoj**:High Density Online Judge(高密度在线评测系统),是...

    HDOJ1000

    ACM ICPC HDOJ1000

    HDOJ DP题目总结

    一些HDOJ上的DP题目的小总结,但愿能帮到那些想专攻DP的人吧

    hdoj1002——大整数相加

    ### hdoj1002——大整数相加 #### 题目背景与目的 本题目来源于杭州电子科技大学的在线评测系统(HDOJ),编号为1002的大整数相加问题。该题目主要考察的是编程者对于大整数处理的基本技巧以及对数组、循环等基础...

    hdoj 2013 多校训练4标程+解题报告

    【标题解析】:“hdoj 2013 多校训练4标程+解题报告”这个标题表明,这是一个关于2013年Happy Dream Online Judge(简称hdoj)组织的多校联合编程训练的资料。"4标程"意味着包含了四道题目(或者可能是四个阶段)的...

    ACM HDOJ 课件

    【ACM HDOJ 课件】是一套涵盖了多种计算机科学竞赛中常见算法与理论的教育资源,主要针对ACM(国际大学生程序设计竞赛)和HDOJ(华中地区大学生在线编程题库)的训练。这些课件深入浅出地讲解了在解决复杂问题时所需...

Global site tag (gtag.js) - Google Analytics