LA 4513

题目链接

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 40000 + 10;
const int x = 123;
int n, m, pos;
unsigned long long H[maxn], xp[maxn];
unsigned long long Hash[maxn];
int Rank[maxn];

int cmp(const int& a, const int& b)
{
    return Hash[a] == Hash[b] ? a < b : Hash[a] < Hash[b];
}

int possible(int L)
{
    int c = 0;
    pos = -1;
    for(int i = 0; i < n - L + 1; i++)
    {
        Rank[i] = i;
        Hash[i] = H[i] - H[i + L] * xp[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;
        if(++c >= m)
            pos = max(pos, Rank[i]);
    }
    //printf("L : %d   pos : %d\n", L, pos);
    return pos >= 0;
}

char s[maxn];

int main()
{
    while(~scanf("%d", &m) && m)
    {
        scanf("%s", s);
        n = strlen(s);
        H[n] = 0;
        for(int i = n - 1; i >= 0; i--) H[i] = H[i + 1] * x + (s[i] - 'a');
        xp[0] = 1;
        for(int i = 1; i <= n; i++) xp[i] = xp[i - 1] * x;
        if(!possible(1)) printf("none\n");
        else
        {
            int L = 1, R = n + 1;
            while(L + 1 < R)
            {
                int mid = (L + R) >> 1;
                if(possible(mid)) L = mid;
                else R = mid;
            }
            possible(L);
            printf("%d %d\n", L, pos);
        }
    }
    return 0;
}


/*

2
babab

*/


你可能感兴趣的:(ACM)