zoukankan      html  css  js  c++  java
  • Stammering Aliens(二分+Hash 卡过)

    Stammering Aliens

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2516    Accepted Submission(s): 822


    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
     
    用unrder_map 大概能快1s多  AC了就没改了
     
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <map>
    #define ull unsigned long long   //溢出部分自动取余
    using namespace std;
    
    int n,len,pos,res;
    const int maxn=4e4+5;
    const int base=133;
    ull p[maxn],Hash[maxn];
    map<ull,int> ma;
    string name;
    
    void init(){
        p[0]=1;
        for(int i=1;i<=maxn-1;i++) p[i]=p[i-1]*base;
    }
    
    ull get(int left,int right,ull g[]){
        return g[right]-g[left-1]*p[right-left+1];
    }
    
    int check(int ee){   //判断这个长度的字符串有没有符合要求的
        ma.clear();
        int ans=-1;
        for(int i=1;i<=len-ee+1;i++){   //从小到大保证最右边的位置
            int zhi=get(i,i+ee-1,Hash);
            ma[zhi]++;
            if(ma[zhi]>=n) ans=i;
        }
        return ans;
    }
    
    bool Binary(int left,int right){
        pos=-1,res=-1;
        while(left<=right){
            int mid=(left+right)/2;
            int num=check(mid);
            if(num!=-1){   //说明有符合条件的
                res=mid;  //长度
                pos=num;
                left=mid+1;
            }
            else right=mid-1;
    
        }
        return res!=-1;
    }
    
    int main(){
        init();
        while(cin>>n,n){
            cin>>name;
            Hash[0]=0;
            len=name.size();
            name=" "+name;
            for(int i=1;i<=len;i++) Hash[i]=Hash[i-1]*base+name[i]-'a';
            int L=1,R=len-n+1;
            bool flag=Binary(L,R);
            if(!flag) cout << "none" << endl;
            else cout << res << " " << pos-1 << endl;
        }
        return 0;
    }
    View Code
     
  • 相关阅读:
    Android Service总结06 之AIDL
    Android Service总结05 之IntentService
    Android Service总结04 之被绑定的服务 -- Bound Service
    Android Service总结02 service介绍
    Android Service总结03 之被启动的服务 -- Started Service
    Android Service总结01 目录
    Android 数据存储04之Content Provider
    Android 数据存储03之SQLite
    Android 数据存储02之文件读写
    Android 数据存储01之SharedPreferences
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11699259.html
Copyright © 2011-2022 走看看