UVA 12206 Stammering Aliens 哈希 + 二分

版权声明:转载请标明出处 https://blog.csdn.net/weixin_41190227/article/details/86491723

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

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3358

题意:输入一个m,出现次数超过m次的最长的子串。

哈希+二分搞定。一直在写哈希的题目,也是写的越来越顺手了。

/*
@Author: Top_Spirit
@Language: C++
*/
//#include <bits/stdc++.h>
//#include <unordered_map>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
const int Maxn = 4e4 + 10 ;
const int seed = 131 ;

string s ;
ull ha[Maxn], _Hash[Maxn], xp[Maxn] ;
int len, n, pos ;
int Rank[Maxn] ;

void init(){
    ha[len] = 0 ;
    for (int i = len - 1; i >= 0; i--) ha[i] = ha[i + 1] * seed + s[i] ;
    xp[0] = 1 ;
    for (int i = 1; i <= len; i++) xp[i] = xp[i - 1] * seed ;
}

bool cmp (int index1, int index2){
    if (_Hash[index1] != _Hash[index2]) return _Hash[index1] < _Hash[index2] ;
    else return index1 < index2 ;
}

int check (int mid){
    for (int i = 0; i < len - mid + 1; i++){
        _Hash[i] = ha[i] - ha[i + mid] * xp[mid] ;
        Rank[i] = i ;
    }
    sort(Rank, Rank + len - mid + 1, cmp) ;
    pos = -1 ;
    int cnt = 0 ;
    for (int i = 0; i < len - mid + 1; i++){
        if (i == 0 || _Hash[Rank[i]] != _Hash[Rank[i - 1]]) cnt = 0 ;
        cnt++ ;
        if (cnt >= n) pos = max(pos, Rank[i]) ;
    }
    return pos >= 0 ;
}

int main (){
    while (cin >> n && n){
        cin >> s ;
        len = s.size() ;
        init() ;
        if (!check(1)) {
            cout << "none" << endl ;
            continue ;
        }
        int left = 0, right = len ;
        int ans = 0 ;
        while (left <= right){
            int mid = (left + right) >> 1 ;
            if (check(mid)) left = mid + 1, ans = mid ;
            else right = mid - 1 ;
        }
//        cout << ans << endl ;
        if (ans) {
            check(ans) ;
            cout << ans << " " << pos << endl ;
        }
        else cout << "none" << endl ;
    }
    return 0 ;
}

关于过去,告一段落

关于未来,敬请期待

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/86491723