zoukankan      html  css  js  c++  java
  • UVALive 6869 Repeated Substrings(后缀数组)

    String analysis often arises in applications from biology and
    chemistry, such as the study of DNA and protein molecules. One
    interesting problem is to find how many substrings are repeated
    (at least twice) in a long string.
    In this problem, you will write a program to find the total
    number of repeated substrings in a string of at most 100 000
    alphabetic characters. Any unique substring that occurs more
    than once is counted. As an example, if the string is “aabaab”,
    there are 5 repeated substrings: “a”, “aa”, “aab”, “ab”, “b”.
    If the string is “aaaaa”, the repeated substrings are “a”, “aa”, “aaa”, “aaaa”. Note that repeated
    occurrences of a substring may overlap (e.g. “aaaa” in the second case).
    Input
    The first line contains a positive integer, specifying the number of cases to follow. Each of the following
    line contains a nonempty string of up to 100 000 alphabetic characters.
    Output
    For each line of input, output one line containing the number of unique substrings that are repeated.
    You may assume that the correct answer fits in a signed 32-bit integer.
    Sample Input
    3
    aabaab
    aaaaa
    AaAaA
    Sample Output
    5
    4
    5

    /*
    后缀数组.
    题意:求出现过两次以上的不同子串有多少种.
    字串即字符串后缀的前缀.
    ans+=ht[i]-ht[i-1].
    用当前的贡献减去之前已经计算过的贡献.
    然后把每个前缀的贡献累加.
    */
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #define MAXN 100010
    using namespace std;
    int n,K,m=130,ans,sa[MAXN],rank1[MAXN],c[MAXN],ht[MAXN],t1[MAXN],t2[MAXN],s[MAXN];
    char ch[MAXN];
    bool cmp(int *y,int a,int b,int k)
    {
        int a1=y[a],b1=y[b];
        int a2=a+k>=n?-1:y[a+k];
        int b2=b+k>=n?-1:y[b+k];
        return a1==b1&&a2==b2;
    }
    void slovesa()
    {
        int *x=t1,*y=t2;
        for(int i=0;i<m;i++) c[i]=0;
        for(int i=0;i<n;i++) c[x[i]=s[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,p=0;k<=n;k<<=1,m=p,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++)
            {
                if(cmp(y,sa[i-1],sa[i],k)) x[sa[i]]=p-1;
                else x[sa[i]]=p++;
            }
            if(p>=n) break;
        }
    }
    void sloveheight()
    {
        int k=0;
        for(int i=0;i<n;i++) rank1[sa[i]]=i;
        for(int i=0;i<n;ht[rank1[i++]]=k)
        {
            int j=sa[rank1[i]-1];
            if(k) k--;
            while(j+k<n&&i+k<n&&s[i+k]==s[j+k]) k++;
        }
        ht[0]=0;
    }
    void slove()
    {
        for(int i=1;i<n;i++)
        {
            int l=n-sa[i-1]-1;
            if(ht[i]>=ht[i-1]) ans+=ht[i]-ht[i-1];
        }
        printf("%d
    ",ans);
    }
    void Clear()
    {
        ans=0,m=130;
        memset(sa,0,sizeof sa);
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%s",ch);Clear();
            n=strlen(ch);
            for(int i=0;i<n;i++) s[i]=ch[i];
            s[n++]=0;
            slovesa(),sloveheight(),slove();
        }
        return 0;
    }
  • 相关阅读:
    51nod-1462: 树据结构
    51nod-1363: 最小公倍数之和
    jar包反编译
    js表格某列多行值相同进行行合并
    JS对象转URL参数
    json数组转字符串 前端与后端交互
    element-ui 表格数据根据某一列值相同进行行合并(包括序号列)并同列相同值进行合计
    ssh框架中联合查询所取结果不在单一实体,sql写法
    ajax异步获取数据后动态向构建表格并添加数据
    关于ajax中async: false的作用
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068012.html
Copyright © 2011-2022 走看看