• codeforces 432D. Prefixes and Suffixes(后缀数组)


    题目链接

    D. Prefixes and Suffixes
    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character.

    Let's introduce several definitions:

    A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string sisi + 1...sj.
    The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l].
    The suffix of string s of length l (1 ≤ l ≤ |s|) is string s[|s| - l + 1..|s|].
    Your task is, for any prefix of string s which matches a suffix of string s, print the number of times it occurs in string s as a substring.

    Input
    The single line contains a sequence of characters s1s2...s|s| (1 ≤ |s| ≤ 105) — string s. The string only consists of uppercase English letters.

    Output
    In the first line, print integer k (0 ≤ k ≤ |s|) — the number of prefixes that match a suffix of string s. Next print k lines, in each line print two integers li ci. Numbers li ci mean that the prefix of the length li matches the suffix of length li and occurs in string s as a substring ci times. Print pairs li ci in the order of increasing li.

    Examples
    input
    ABACABA
    output
    3
    1 4
    3 2
    7 1
    input
    AAA
    output
    3
    1 3
    2 2
    3 1

    题意:给出一个长度$ 1 leq |s| leq 10^5$ 的字符串,问有多少种长度 (l) ,使得长度 (l) 的前缀和后缀相等,并且问这个长度为 (l) 的字符串多少次作为字串出现在 (s) 中。

    题解:这题使用后缀数组,假设后缀 (suffix(0)) (即整个字符串)的 (rank)(x)(l=|s|),那么从 (x) 向后扫到 (l) ,记录区间 (height) 最小值 (mi) ,若(mi) 等于该后缀 (sa[i]) 的长度,那么可以作为答案,同时在扫的过程中,(ans[mi]++) ,( (ans[i]) 表示和字符串 (s) 的长度为 (i) 的前缀作为字串出现过多少次),同理从 (x) 往前扫一遍。

    由于(ans[i]) 表示长度刚好为 (i) 的次数,长度为 (i+1) 也满足题意,那么记录一个后缀和就行了。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<stack>
    using namespace std;
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=n-1;i>=a;i--)
    #define pb push_back
    #define fi first
    #define se second
    typedef vector<int> VI;
    typedef long long ll;
    typedef pair<int,int> PII;
    const int inf=0x3fffffff;
    const ll mod=1000000007;
    const int maxn=2e5+100;
    /*
     *suffix array
     *倍增算法  O(n*logn)
     *待排序数组长度为n,放在0~n-1中,在最后面补一个0
     *build_sa( ,n+1,m+1); //注意是n+1,m是s数组中的最大值;
     *getHeight(,n);
     *例如:
     *n   = 8;
     *num[]   = { 1, 1, 2, 1, 1, 1, 1, 2, $ };注意num最后一位为0,其他大于0
     *rank[]  = { 4, 6, 8, 1, 2, 3, 5, 7, 0 };rank[0~n-1]为有效值,rank[n]必定为0无效值
     *sa[]    = { 8, 3, 4, 5, 0, 6, 1, 7, 2 };sa[1~n]为有效值,sa[0]必定为n是无效值
     *height[]= { 0, 0, 3, 2, 3, 1, 2, 0, 1 };height[2~n]为有效值
     *
     */
    
    int sa[maxn];//SA数组,表示将S的n个后缀从小到大排序后把排好序的
    //的后缀的开头位置顺次放入SA中
    int t1[maxn],t2[maxn],c[maxn];//求SA数组需要的中间变量,不需要赋值
    int rk[maxn],height[maxn];
    //待排序的字符串放在s数组中,从s[0]到s[n-1],长度为n,且最大值小于m,
    //除s[n-1]外的所有s[i]都大于0,r[n-1]=0
    //函数结束以后结果放在sa数组中
    void build_sa(int s[],int n,int m)
    {
        int i,j,p,*x=t1,*y=t2;
        //第一轮基数排序,如果s的最大值很大,可改为快速排序
        for(i=0;i<m;i++)c[i]=0;
        for(i=0;i<n;i++)c[x[i]=s[i]]++;
        for(i=1;i<m;i++)c[i]+=c[i-1];
        for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
        for(j=1;j<=n;j<<=1)
        {
            p=0;
            //直接利用sa数组排序第二关键字
            for(i=n-j;i<n;i++)y[p++]=i;//后面的j个数第二关键字为空的最小
            for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
            //这样数组y保存的就是按照第二关键字排序的结果
            //基数排序第一关键字
            for(i=0;i<m;i++)c[i]=0;
            for(i=0;i<n;i++)c[x[y[i]]]++;
            for(i=1;i<m;i++)c[i]+=c[i-1];
            for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
            //根据sa和x数组计算新的x数组
            swap(x,y);
            p=1;x[sa[0]]=0;
            for(i=1;i<n;i++)
                x[sa[i]]=y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
            if(p>=n)break;
            m=p;//下次基数排序的最大值
        }
    }
    void getHeight(int s[],int n)
    {
        int i,j,k=0;
        for(i=0;i<=n;i++) rk[sa[i]]=i;
        for(i=0;i<n;i++)
        {
            if(k)k--;
            j=sa[rk[i]-1];
            while(s[i+k]==s[j+k])k++;
            height[rk[i]]=k;
        }
    }
    int f[maxn],ans[maxn];
    char s[maxn];
    int a[maxn];
    
    
    int main()
    {
        scanf("%s",s);
        int l=strlen(s);
        rep(i,0,l) a[i]=s[i]-'A'+1;
        s[l]=0;
        build_sa(a,l+1,30);
        getHeight(a,l);
        int x=rk[0];
        int mi=inf;
        for(int i=x+1;i<=l;i++)
        {
            mi=min(mi,height[i]);
            if(mi+sa[i]==l)  f[mi]=1;
            ans[mi]++;
        }
        mi=inf;
        for(int i=x-1;i>0;i--)
        {
            mi=min(mi,height[i+1]);
            if(mi+sa[i]==l)  f[mi]=1;
            ans[mi]++;
        }
        int cnt=0;
        f[l]=ans[l]=1;
        per(i,1,l+1) ans[i]+=ans[i+1],cnt+=f[i];
        printf("%d
    ",cnt);
        rep(i,1,l+1) if(f[i]) printf("%d %d
    ",i,ans[i]);
        return 0;
    }
    
    
    
  • 相关阅读:
    ASP.NET MVC中防止跨站请求攻击(CSRF)
    C#操作JSON学习
    C# 产生随机密码
    博客园上好的技术系列收藏
    OWIN学习
    bzoj1068: [SCOI2007]压缩
    bzoj1012: [JSOI2008]最大数maxnumber
    bzoj1055: [HAOI2008]玩具取名
    bzoj1011: [HNOI2008]遥远的行星
    bzoj1008: [HNOI2008]越狱
  • 原文地址:https://www.cnblogs.com/tarjan/p/7398097.html
Copyright © 2020-2023  润新知