UVA 11107 Life Forms(后缀数组 OR hash)

题目大意:给你一个字符串,问你这个字符串中出现次数不超过m的长度最大的子串的最右边的起始位置。
思路:书上的例题,二分答案是肯定的。由于后缀数很小,那么书上的方法是直接 hash 。但是我感觉 hash 的话,直接这样做,如果运气不好会WA,写起来的确是hash快,而且不易敲错。。 = =
另外一种方法就是直接后缀数组了,挺基础的一题。。

hash法代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef unsigned long long ulld;

const int MAXN = 41111;

char str[MAXN];

int n;

ulld hash[MAXN],exp[MAXN];

void get_hash()
{
    ulld seed = 131;
    hash[n] = 0;
    for(int i = n - 1;i>=0;i--)
    {
        hash[i] = hash[i+1]*seed + str[i] - 'a';
    }

    exp[0] = 1;
    for(int i = 1;i<=n;i++)
        exp[i] = exp[i- 1] * seed;
}



struct HH
{
    ulld hash;
    int pos;
}hh[MAXN];

int cmp(HH a,HH b)
{
    if(a.hash == b.hash)
        return a.pos < b.pos;
    else return a.hash < b.hash;
}

int ans_pos;

int m;

int check(int mid)
{
    for(int i = 0;i<n;i++)
    {
        if(i + mid > n) break;
        hh[i].hash = hash[i] - hash[i+mid]*exp[mid];
        hh[i].pos = i;
    }
    sort(hh,hh + n - mid + 1,cmp);
    int cc = 0;
    int ok = 0;
    int pos = -1;
    for(int i = 0;i<n - mid + 1;i++)
    {

        if(i == 0 || hh[i].hash == hh[i-1].hash) cc++;
        else cc = 1;
        if(cc >= m)
        {
            ok = 1;
            pos = max(hh[i].pos,pos);
        }
    }
    if(ok) ans_pos = pos;
    return ok;
}

int main()
{
    while(~scanf("%d",&m) && m)
    {
        scanf("%s",str);
        n = strlen(str);
        get_hash();
        int l = 1,r = n;
        int ans = 0;
        while(l <= r)
        {
            int mid = (l+r) >> 1;
            if(check(mid))
            {
                ans = mid;
                l = mid + 1;
            }
            else r = mid - 1;
        }
        if(ans)
        {
            printf("%d %d\n",ans,ans_pos);
        }
        else puts("none");
    }
    return 0;
}


后缀数组代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN = 41111;

char str[MAXN];

int sa[MAXN],t[MAXN],t2[MAXN],c[MAXN];

void build_sa(int n,int m)
{
    int *x = t,*y = t2;
    for(int i = 0;i<m;i++) c[i] = 0;
    for(int i = 0;i<n;i++) c[x[i] = str[i]]++;
    for(int i = 1;i<m;i++) c[i] += c[i-1];
    for(int i = n - 1;i>=0;i--) sa[--c[x[i]]] = i;
    for(int k = 1;k <= n; k <<= 1)
    {
        int p  = 0;
        for(int i = n - k ;i<n;i++) y[p++] = i;
        for(int i = 0;i<n;i++) if(sa[i] >= k) y[p++] = sa[i] - k;
        for(int i = 0;i<m;i++) c[i] = 0;
        for(int i = 0;i<n;i++) c[x[y[i]]]++;
        for(int i = 1;i<m;i++) c[i] += c[i-1];
        for(int i = n-1;i >= 0;i--) sa[--c[x[y[i]]]] = y[i];
        swap(x,y);
        p = 1;
        x[sa[0]] = 0;
        for(int i = 1;i < n;i++)
            x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1] + k] == y[sa[i] + k] ? p - 1 : p++;
        if(p >= n) break;
        m = p;
    }
    //for(int i = 1;i <= n;i++)
        //printf("i = %d,sa = %d\n",i,sa[i]);
}

int  height[MAXN],rank[MAXN];

void get_height(int n)
{
    for(int i = 1;i <= n;i++) rank[sa[i]] = i;
    int k = 0;
    for(int i = 0;i<n;i++)
    {
        if(k) k--;
        int j = sa[rank[i] - 1];
        while(str[i+k] == str[j+k]) k++;
        height[rank[i]] = k;
    }
    //for(int i = 1;i <= n;i++)
        //printf("i = %d,hi = %d\n",i,height[i]);
}

int ans_pos;

int check(int mid,int m,int n)
{
    int pos = -1;
    int cc = 0;
    int tmp = -1;
    for(int i = 1;i <= n;i++)
    {
        if(i == 1)
        {
            if(n - sa[i] >= mid)
            {
                cc = 1;
                tmp = sa[i];
            }
            else
            {
                cc = 0;
                tmp = -1;
            }
        }
        else if(height[i] >= mid)
        {
            tmp = max(sa[i],tmp);
            cc++;
        }
        else
        {
            if(n - sa[i] >= mid)
            {
                cc = 1;
                tmp = sa[i];
            }
            else
            {
                cc = 0;
                tmp = -1;
            }
        }
        if(cc >= m) pos = max(pos,tmp);

    }
    if(pos != -1)
    {
        ans_pos = pos;
        return 1;
    }
    else return 0;
}

int main()
{
    int m;
    while(~scanf("%d",&m) && m)
    {
        scanf("%s",str);
        int n = strlen(str);
        build_sa(n+1,256);
        get_height(n);
        int l = 1,r = n;
        int ans = 0;
        while( l <= r)
        {
            int mid = (l + r) >> 1;
            if(check(mid,m,n))
            {
                ans = mid;
                l = mid + 1;
            }
            else r = mid - 1;
        }

        if(ans)
        {
            printf("%d %d\n",ans,ans_pos);
        }
        else puts("none");
    }
    return 0;
}

/*
3
abcabcabc

*/


你可能感兴趣的:(UVA 11107 Life Forms(后缀数组 OR hash))