zoukankan      html  css  js  c++  java
  • UVALive 4513 Stammering Aliens 字符串Hash求LCS

    Description

    Download as PDF
    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 57 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$ ge$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 possible starting position of such a substring.

    Sample Input

    3
    baaaababababbababbab
    11
    baaaababababbababbab
    3
    cccccc
    0
    

    Sample Output

    5 12
    none
    4 2
    

    ------

    --------

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    
    typedef unsigned long long ULL;
    
    const int SIZE = 100003;
    const int SEED = 13331;
    const int MAX_N = 50000 + 10;
    char s[MAX_N];
    struct HASH{
        ULL H[MAX_N];
        ULL XL[MAX_N];
        int len;
        HASH(){}
        void build(char *s){
            len=strlen(s);
            H[len]=0;
            XL[0]=1;
            for (int i=len-1;i>=0;i--){
                H[i]=H[i+1]*SEED+s[i];
                XL[len-i]=XL[len-i-1]*SEED;
            }
        }
        ULL hash(int i,int L){
            return H[i]-H[i+L]*XL[L];
        }
    }hs;
    int m,n;
    int pos;
    int rank[MAX_N];
    ULL hash[MAX_N];
    int cmp(const int & a, const int & b){
        return hash[a]<hash[b]||(hash[a]==hash[b]&&a<b);
    }
    bool possible(int L)
    {
        int c=0;
        pos=-1;
        for (int i=0;i<n-L+1;i++)
        {
            rank[i]=i;
            hash[i]=hs.hash(i,L);
        }
        sort(rank,rank+n-L+1,cmp);
        for (int i=0;i<n-L+1;i++)
        {
            if (i==0||hash[rank[i]]!=hash[rank[i-1]]) c=0;
            c++;
            if (c>=m) pos=max(pos,rank[i]);
        }
        return pos>=0;
    }
    
    int main()
    {
        while (~scanf("%d",&m))
        {
            if (m==0) break;
            scanf("%s",s);
            n=strlen(s);
            hs.build(s);
            if (!possible(1)) printf("none
    ");
            else{
                int L=1,R=n;
                int ans=L;
                while (L<=R)
                {
                    int M=L+(R-L)/2;
                    if (possible(M)){
                        ans=M;
                        L=M+1;
                    }else{
                        R=M-1;
                    }
                }
                possible(ans);
                printf("%d %d
    ",ans,pos);
            }
        }
        return 0;
    }
    





  • 相关阅读:
    Dump 文件生成与分析
    打造支持apk下载和html5缓存的 IIS(配合一个超简单的android APP使用)具体解释
    GridView编辑删除操作
    google域名邮箱申请 gmail域名邮箱申请(企业应用套件)指南
    nvl,空时的推断和取值
    如何将图片保存至自定义分组
    Java实现 蓝桥杯VIP 算法训练 集合运算
    Java实现 蓝桥杯VIP 算法训练 瓷砖铺放
    Java实现 蓝桥杯VIP 算法训练 瓷砖铺放
    Java实现 蓝桥杯VIP 算法训练 集合运算
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681591.html
Copyright © 2011-2022 走看看