zoukankan      html  css  js  c++  java
  • [poj][3261][Milk Patterns]

    题目:http://poj.org/problem?id=3261

    第一道后缀数组。二分答案,然后遍历height数组,判断该答案是否出现次数大于k次。

    View Code
    #include <cstdio>
    #include <cstring>
    #include <map>
    
    using namespace std;
    
    const int N = 1000000+10;
    int ua[N], ub[N], us[N], n, m, arr[N], sa[N], t, cnt;
    int cmp(int *r,int a,int b,int l){
        return r[a]==r[b]&&r[a+l]==r[b+l];
    }
    void da(int *r,int n,int m){
        int i,j,p,*x=ua,*y=ub,*t;
        for (i=0;i<m;i++) us[i]=0;
        for (i=0;i<n;i++) us[x[i]=r[i]]++;
        for (i=1;i<m;i++) us[i]+=us[i-1];
        for (i=n-1;i>=0;i--) sa[--us[x[i]]]=i;
        for (j=1,p=1;p<n;j*=2,m=p){
            for (p=0,i=n-j;i<n;i++) y[p++]=i;
            for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
            for (i=0;i<m;i++) us[i]=0;
            for (i=0;i<n;i++) us[x[i]]++;
            for (i=1;i<m;i++) us[i]+=us[i-1];
            for (i=n-1;i>=0;i--) sa[--us[x[y[i]]]]=y[i];
            for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
                x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
    }
    int rank[N],height[N];
    void calheight(int *r,int n){
        int i,j,k=0;
        for (i=1;i<=n;i++)rank[sa[i]]=i;
        for (i=0;i<n;height[rank[i++]]=k)
            for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
    }
    void solve(int k){
        int i, tot, maxx=0;
        int low=1, high=n, mid;
        while (low <= high){
            mid = low + (high - low) / 2;
            for (tot=1, i=2; i<=n; i++){
                if (height[i]>=mid)tot++;
                else tot = 1;
                if (tot >= k) break;
            }
            if (tot >= k)low=mid+1,maxx=mid;
            else high=mid-1;
        }
        printf("%d\n", maxx);
    }
    
    int main(){
        //freopen("D:/a.txt", "r", stdin);
        while (~scanf("%d%d", &n, &m)){
            memset(arr, 0, sizeof(arr));
            map<int,int>ma;
            cnt = 1;
            for (int i=0; i<n; i++){
                scanf("%d", &t);
                if (ma.find(t)==ma.end())ma[t]=cnt++;
                arr[i] = ma[t];
            }
            arr[n] = 0;
            da(arr, n+1, cnt); //算后缀数组的时候要把最后一个算在内,排名才是从1开始的,因为空串排在第0个。
            calheight(arr, n);
            solve(m);
        }
        return 0;
    }
  • 相关阅读:
    282. Expression Add Operators
    281. Zigzag Iterator
    280. Wiggle Sort
    How Not to Crash #2: Mutation Exceptions 可变异常
    Moving Swiftly
    How to Use updateConstraints
    Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中
    Where-To-Put-The-Auto-Layout-Code
    iOS five years[转]
    ResponderChain note
  • 原文地址:https://www.cnblogs.com/nigel0913/p/2591491.html
Copyright © 2011-2022 走看看