zoukankan      html  css  js  c++  java
  • 数据结构--后缀数组-HDU 5769

    Substring

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1686    Accepted Submission(s): 657


    Problem Description
    ?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings. 
    But ?? thinks that is too easy, he wants to make this problem more interesting. 
    ?? likes a character X very much, so he wants to know the number of distinct substrings which contains at least one X. 
    However, ?? is unable to solve it, please help him.
     
    Input
    The first line of the input gives the number of test cases T;T test cases follow. 
    Each test case is consist of 2 lines: 
    First line is a character X, and second line is a string S. 
    X is a lowercase letter, and S contains lowercase letters(‘a’-‘z’) only.

    T<=30 
    1<=|S|<=10^5 
    The sum of |S| in all the test cases is no more than 700,000.
     
    Output
    For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the answer you get for that case.
     
    Sample Input
    2 a abc b bbb
     
    Sample Output
    Case #1: 3 Case #2: 3
    Hint
    In first case, all distinct substrings containing at least one a: a, ab, abc. In second case, all distinct substrings containing at least one b: b, bb, bbb.
     
    Author
    FZU
     
    Source
     
    Recommend
    wange2014   |   We have carefully selected several similar problems for you:  6253 6252 6251 6250 6249 
     
    题意:这个题目的意思就是要求你求出包含一个特定的字符的字串有多少个,题目会给出一个字符串同时会给出一个特定的字符,要求你回答上述的问题
     
    思路:这个题的思路就是使用后缀数组。因为题目的要求就是希望知道包含某一个特定的字符所能组成的子串的个数有多少个,那么我们可以标记每一个在字符串中出现的特殊字符,然后标记这个字符的作用就是希望能够确定包含这个字符的子串的个数。那么我们要怎么去确定这样符合要求的子串呢,可以通过sa数组和height数组来进行统计,通过后缀数组得出这两个数组之后,发现包含特殊字符的子串就分成了两种情况,那就是特殊字符在两个后缀的最长公共前缀中,那么这样的情况下包含的子串的个数就是字符串的长度减去最长公共前缀的长度,注意这里不是直接减去这个长度,而是在后缀的基础上减去这一个长度。
         另一种情况就是特殊字符不在两个后缀的最长公共前缀中,那么这个时候就需要我们用整个字符串的长度减去这个特殊字符所在的位置即可,得出来的就是所有可能的情况了。
    #include<cstdio>
    #include<iostream>
    #include<queue>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<cstdlib>
    #include<bitset>
    #include<map>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    const int maxn=100010;
    const int mod=100000007;
    const double PI = acos(-1.0);
    const double eps = 1e-6;
    char s[maxn];
    int str[maxn];
    int Rank[maxn],height[maxn];
    int sa[maxn],t[maxn],t2[maxn],c[maxn];
    int local[maxn];
    void build_sa(int * s,int n,int m)
    {
        int i,*x = t,*y = t2;
        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(int k = 1;k <= n;k <<= 1)
        {
            int p = 0;
            for(i = n - k;i < n;i++)    y[p++] = i;
            for(i = 0;i < n;i++)    if(sa[i] >= k)    y[p++] = sa[i] - k;
            for(i = 0;i < m;i++)    c[i] = 0;
            for(i = 0;i < n;i++)    c[ x[y[i]] ]++;
            for(i = 0;i < m;i++)    c[i] += c[i-1];
            for(i = n-1;i >= 0;i--)    sa[--c[x[y[i]]]] = y[i];
            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] + k] == y[sa[i] + k] ? p-1:p++;
            if(p >= n)    break;
            m = p;
        }
    }
    void calheight(int * s,int n)
    {
        int i,j,k = 0;
        for(i = 0;i <= n;i++)Rank[sa[i]] = i;
        for(i = 0;i < n;i++)
        {
            if(k) k--;
            int j = sa[Rank[i]-1];
            while(s[i+k] == s[j+k])    k++;
            height[Rank[i]] = k;
        }
    }
    ll solve(int n)
    {
        int i,j,k=0;
        ll sum=0;
        for(i=1;i<=n;i++)
        {
            sum+=(ll)(n-max(sa[i]+height[i],local[sa[i]]));
        }
        return sum;
    }
    int main()
    {
            int t;
            scanf("%d",&t);
            for(int g=0;g<t;g++)
            {
                int i,j;
                char th;
                int num;
                getchar();
                scanf("%c",&th);
                scanf("%s",s);
                num=strlen(s);
                for(i=0;i<num;i++)
               {
                   str[i]=s[i]-'a'+1;
               }
               str[num]=0;
               local[num]=num;
               for(i=num-1;i>=0;i--)
               {
                   if(s[i]==th)
                   {
                       local[i]=i;
                   }
                   else
                   {
                       local[i]=local[i+1];
                   }
               }
               build_sa(str,num+1,30);
               calheight(str,num);
               ll num1=solve(num);
               printf("Case #%d: ",g+1);
               printf("%I64d
    ",num1);
            }
        return 0;
    }
    

      

     
     
     
     
     
  • 相关阅读:
    Daily Scrum NO.4
    Daily Scrum NO.3
    Daily Scrum NO.2
    Daily Scrum NO.1
    Beta任务项录入
    M1事后分析报告
    wenbao与概率
    wenbao与组合数
    wenbao与高斯消元
    wenbao与链表
  • 原文地址:https://www.cnblogs.com/yewa/p/8303355.html
Copyright © 2011-2022 走看看