zoukankan      html  css  js  c++  java
  • UVALive

    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 possible starting position of such a
    substring.

    Sample Input

    3
    baaaababababbababbab
    11
    baaaababababbababbab
    3
    cccccc
    0

    Sample Output

    5 12
    none
    4 2

    题意:给定一个字符串,找出重复出现超过m次的字串的最大开始下标
    思路: Hash + 二分 对字符串的长度进行二分 时间复杂度 O( n*lgn )

    AC code:

    /*给定一个字符串,找出重复出现超过m次的字串的最大开始下标*/
    #include <map>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    typedef unsigned long long llu;
    
    const int maxn = 4e4+50;
    
    int len ,t ;
    llu seed = 131,Hash[maxn],temp[maxn];
    char str[maxn];
    
    void init() {
        Hash[0] = 1;
        for (int i = 1;i <= len;i++) {
            Hash[i] = Hash[i-1]*seed + (str[i]-'a'+1);
        }
    }
    
    llu get(int l,int r) {
        return Hash[r] - Hash[l-1] * temp[r-l+1] ;
    }
    
    bool judge (int length) {
        map<llu,int>mp;
        for (int i = length;i <= len ; i ++ ) {
            if( ++mp[get(i-length+1,i)] >= t ) {
                return true;
            }
        }
        return false;
    }
    
    int main(){
        temp[0] = 1;
        for (int i = 1;i<=maxn;i++) {
            temp[i] = temp[i-1] * seed;
        }
        while ( ~scanf("%d",&t) && t ) {
            scanf("%s",str+1);
            len = strlen(str+1);
            init();
            int l = 1 ,r = len ,mid ;
            if ( !judge(l) ) { printf("none
    ");  continue; }
            while ( l <= r ) {
                mid = (l + r)>>1;
                if ( judge(mid) ) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
            int ans;
            map<llu,int>mp;
            for (int i = r ;i <= len;i++) {
            //printf("%d %llu
    ",i,get(i-r+1,i));
                if ( ++mp[get(i-r+1,i)] >= t ) {
                    ans = i-r;
                }
            }
            printf("%d %d
    ",r,ans);
        }
        return 0;
    }
    /*
    3
    baaaababababbababbab
    11
    baaaababababbababbab
    3
    cccccc
    0
    */
    
  • 相关阅读:
    SpringCloud学习第四篇:Feign学习(Hoxton.SR4)
    SpringCloud学习第三篇:Ribbon负载均衡(Hoxton.SR4)
    SpringCloud学习第二篇:使用Consul注册中心(Greenwich-SR1版本)
    SpringCloud学习第一篇:服务的注册与发现Eureka(Hoxton.SR4)
    SpringBoot+Mycat+APO实现读写分离
    SpringBoot+activeMq
    自动化测试适用场景和难点
    软件测试理论
    软件测试理论中的注意事项
    python自动化:monkey测试的云测
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745945.html
Copyright © 2011-2022 走看看