LA 4513

来源:互联网 发布:网络神剧2015 编辑:程序博客网 时间:2024/04/28 15:52

题目链接

#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;}/*2babab*/


0 0
原创粉丝点击