hdu 3518 后缀数组

(http://www.elijahqi.win/2017/07/21/hdu-3518-%E5%90%8E%E7%BC%80%E6%95%B0%E7%BB%84/)
Boring counting

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

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

Source
2010 ACM-ICPC Multi-University Training Contest(9)——Host by HNU
看到自己上一次写还是在2016.04.10那时候滕老师,带着写的,我还在冲刺中考,现在物是人非,我已经高二了,一晃时间过的真快,接下来的一年 我得在竞赛上努力努力,给我的oi生涯一个完美的句号(也许不是句号hh)课内高考也不能放弃啊。真的有时候就觉得挺累的(。・∀・)ノ不过感觉没有荒废生命还是很开心的

写的时候又在后缀数组调试了很久,ans用int最后也还是ac了
最后的话,就是还是一个height的妙用
题意:找出一个字符串中至少重复出现两次的字串的个数,注意重复出现时不能有重叠的现象
我们循环长度 然后去检验这个长度可以有多少后缀满足
每次记下满足这个长的的min和max 用max-min就是他们的差值 如果大于我们枚举的len那么ans++否则更新一些Min和max
枚举的话枚举到一半长度就可以,加入某一长度不满足那么后面一定也不满足(根据height的性质)

#include
#include
#define N 1100
char a[N];
int count[N],rank[N<<1],rank1[N],tmp[N],sa[N],height[N],m,k;
inline int min(int x,int y){
    return xint max(int x,int y){
    return x>y?x:y;
}
int main(){
    freopen("hdu3518.in","r",stdin);
    freopen("hdu3518.out","w",stdout);
    while (1){
        scanf("%s",a+1);
        if (a[1]=='#') break;
        int n=strlen(a+1);
        memset(count,0,sizeof(count));
        memset(rank,0,sizeof(rank));
        for (int i=1;i<=n;++i) count[a[i]]=1;
        for (int i=1;i<=255;++i) count[i]+=count[i-1];
        for (int i=1;i<=n;++i) rank[i]=count[a[i]];
        k=0;
        for (int p=1;k!=n;p<<=1){
            memset(count,0,sizeof(count));
            for (int i=1;i<=n;++i) count[rank[i+p]]++;
            for (int i=1;i<=n;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) tmp[count[rank[i+p]]--]=i;
            memset(count,0,sizeof(count));
            for (int i=1;i<=n;++i) count[rank[i]]++;
            for (int i=1;i<=n;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) sa[count[rank[tmp[i]]]--]=tmp[i];
            memcpy(rank1,rank,sizeof(rank)>>1);
            rank[sa[1]]=k=1;
            for (int i=2;i<=n;++i){
                if (rank1[sa[i]]!=rank1[sa[i-1]]||rank1[sa[i-1]+p]!=rank1[sa[i]+p]) ++k;
                rank[sa[i]]=k;
            }
        }
        k=0;
    //  for (int i=1;i<=n;++i) printf("%d ",rank[i]);printf("\n");
        for (int i=1;i<=n;++i){
            if (rank[i]==1) continue;
            k=k==0?0:k-1;
            while (a[i+k]==a[sa[rank[i]-1]+k]) ++k;
            height[rank[i]]=k;
        }
        //for (int i=1;i<=n;++i) printf("%d ",height[i]);printf("\n");
        int minn,maxn;bool flag;int ans=0;
        for (int i=1;i<=n>>1;++i){
            flag=false;minn=sa[1],maxn=sa[1];
            for (int j=2;j<=n;++j){
                if (height[j]>=i){
                    minn=min(minn,sa[j]);maxn=max(maxn,sa[j]);
                }else{
                    if (maxn-minn>=i){
                        ++ans;flag=true;
                    }
                    minn=sa[j],maxn=sa[j];
                }
            }
            if (maxn-minn>=i){
                ++ans;flag=true;
            }
            if (!flag) break;
        }
        printf("%d\n",ans);
    }
    return 0;
}

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