zoukankan      html  css  js  c++  java
  • CSU-1632 Repeated Substrings (后缀数组)

    Description

    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 input consists of at most 10 cases. 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

    题目大意:统计字符串中重复出现的子串数目。
    题目分析:sum(max(height(i)-height(i-1),0))即为答案。

    代码如下:
    //# define AC
    
    # ifndef AC
    
    # include<iostream>
    # include<cstdio>
    # include<cstring>
    # include<vector>
    # include<queue>
    # include<list>
    # include<cmath>
    # include<set>
    # include<map>
    # include<string>
    # include<cstdlib>
    # include<algorithm>
    using namespace std;
    # define mid (l+(r-l)/2)
    
    typedef long long LL;
    typedef unsigned long long ULL;
    
    const int N=100000;
    const int mod=1e9+7;
    const int INF=0x7fffffff;
    const LL oo=0x7fffffffffffffff;
    
    int SA[N+5];
    int tSA[N+5];
    int cnt[N+5];
    int rk[N+5];
    int *x,*y;
    int height[N+5];
    
    int idx(char c)
    {
        if('a'<=c&&c<='z') return c-'a';
        return c-'A'+26;
    }
    
    bool same(int i,int j,int k,int n)
    {
        if(y[i]-y[j]) return false;
        if(i+k<n&&j+k>=n) return false;
        if(i+k>=n&&j+k<n) return false;
        return y[i+k]==y[j+k];
    }
    
    void buildSA(char *s)
    {
        int n=strlen(s);
        int m=52;
        x=rk,y=tSA;
        for(int i=0;i<m;++i) cnt[i]=0;
        for(int i=0;i<n;++i) ++cnt[x[i]=idx(s[i])];
        for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
        for(int i=n-1;i>=0;--i) SA[--cnt[x[i]]]=i;
    
        for(int k=1;k<=n;k<<=1){
            int 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) cnt[i]=0;
            for(int i=0;i<n;++i) ++cnt[x[y[i]]];
            for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
            for(int i=n-1;i>=0;--i) SA[--cnt[x[y[i]]]]=y[i];
    
            p=1;
            swap(x,y);
            x[SA[0]]=0;
            for(int i=1;i<n;++i)
                x[SA[i]]=same(SA[i],SA[i-1],k,n)?p-1:p++;
            if(p>=n) break;
            m=p;
        }
    }
    
    void getHeight(char *s)
    {
        int n=strlen(s);
        for(int i=0;i<n;++i) rk[SA[i]]=i;
        int k=0;
        for(int i=0;i<n;++i){
            if(rk[i]==0){
                height[rk[i]]=k=0;
            }else{
                if(k) --k;
                int j=SA[rk[i]-1];
                while(i+k<n&&j+k<n&&s[i+k]==s[j+k])
                    ++k;
                height[rk[i]]=k;
            }
        }
    }
    
    char str[N+5];
    
    void solve()
    {
        int n=strlen(str);
        int ans=0;
        for(int i=0;i<n;++i){
            if(height[i]>height[i-1])
                ans+=height[i]-height[i-1];
        }
        printf("%d
    ",ans);
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%s",str);
            buildSA(str);
            getHeight(str);
            solve();
        }
        return 0;
    }
    
    # endif
    

      

  • 相关阅读:
    摄影技巧:如何拍好夜景?这些拍摄要点值得借鉴
    单反摄影:快门优先怎么用?
    摄影基础知识:什么是光圈优先?
    【震惊】、【无耻】、【嚣张】浙江谷誉科技旗下爱卡之家,黑商圈钱跑路,强行黑吃,用户损失累计数亿
    爱卡之家是不是骗人的,爱卡之家跑路了吗?
    浙江谷誉网络的爱卡之家怎么样,是不是真实的,靠不靠谱?
    爱卡之家app怎么样?爱卡之家油卡套餐可信吗?爱卡之家是不是骗人的,靠不靠谱?
    爱卡之家充值不到账 爱卡之家疑似跑路 爱卡之家客服联系不上
    android TypedValue.applyDimension()的作用
    Android 在xml中配置 float 和 integer 值
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/6092208.html
Copyright © 2011-2022 走看看