HDU 3518 Boring Counting 后缀数组

分组height?半个月没动后缀数组一堆名词都忘了233

#include <cstdio>
#include <cstring>
#include <algorithm>
#define rep(i,j,k) for(i=j;i<k;i++)
#define FOR(i,j,k) for(i=j;i<=k;i++)
using std::min;
using std::max;
#define N 1005
int wa[N], wb[N], sa[N], bucket[N], height[N], rank[N], z[N];
char s[N];

void sort(int *x, int *y, int n, int m) {
    int i;
    rep(i,0,m) bucket[i]=0;
    rep(i,0,n) bucket[x[y[i]]]++;
    rep(i,1,m) bucket[i]+=bucket[i-1];
    for(i=n-1;i>=0;i--) sa[--bucket[x[y[i]]]]=y[i];
}

void da(int n, int m) {
    int i, j, p, *x=wa, *y=wb, *t;
    rep(i,0,n) x[i]=s[i],z[i]=i;
    sort(x,z,n,m);
    for(j=1,p=1;p<n;j*=2,m=p) {
        p=0;
        rep(i,n-j,n) y[p++]=i;
        rep(i,0,n) if(sa[i]>=j) y[p++]=sa[i]-j;
        sort(x,y,n,m);
        t=x,x=y,y=t;p=1;x[sa[0]]=0;
        rep(i,1,n) x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]?p-1:p++;
    }
}

void calHeight(int n) {
    int i,j,k=0;
    for(i=1;i<=n;i++) rank[sa[i]]=i;
    rep(i,0,n) {
        if(k) k--;
        j=sa[rank[i]-1];
        while(s[i+k]==s[j+k]) k++;
        height[rank[i]]=k;
    }
}

int main() {
    while(scanf("%s", s) && s[0]!='#') {
        int n=strlen(s),ans=0,h,j;
        da(n+1,250); calHeight(n);
        FOR(h,1,n/2) {
            int l=n+2,r=-1;
            FOR(j,2,n) {
                if(height[j]>=h) {
                    l=min(min(sa[j],sa[j-1]),l);
                    r=max(max(sa[j],sa[j-1]),r);
                } else {
                    if(l+h<=r) ans++;
                    l=n+2,r=-1;
                }
            }
            if(l+h<=r) ans++;
        }
        printf("%d\n", ans);
    }

    return 0;
}

Boring counting

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2584 Accepted Submission(s): 1047

Problem Description

035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other.
Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from [0-2](The position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).

Input

The input data consist with several test cases.The input ends with a line “#”.each test case contain a string consists with lower letter,the length n won’t exceed 1000(n <= 1000).

Output

For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.

Sample Input

aaaa
ababcabb
aaaaaa
#

Sample Output

2
3
3

你可能感兴趣的:(后缀数组,HDU,OI)