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;
    }
    





  • 相关阅读:
    WSS页面定制系列(1)如何启用表单页面的编辑模式
    [转载]Customizing the Context Menu of Document Library Items
    MOSS publishing功能:创建页面到子文件夹
    CodeArt.SharePoint.CamlQuery_0.9发布(源码)
    WSS页面定制系列(2)定制单个列表的表单页面
    WSS页面定制系列(3)重写表单的保存逻辑
    MOSS字段编辑权限控制方案(3)重写表单字段呈现逻辑
    WSS(MOSS)如何修改Rich文本编辑器的宽度
    web录音的实现
    WSS 扩展文件夹的属性如何给文件夹添加扩展字段
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681591.html
Copyright © 2011-2022 走看看