• 2016暑假多校联合---Substring(后缀数组)


    2016暑假多校联合---Substring

    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

    题意:输入字符x和一个字符串,求包含字符x的不同子串的个数;

    思路: 后缀数组sum=length-(sa[i]+height[i])[i从1~length]  sum即为子串个数,稍作修改,用nxt[i]表示在i右侧距离i最近的字符x的坐标,则

    sum=length-max(nxt[sa[i]],(sa[i]+height[i]))  [i从1~length]就是所求结果;

    代码如下:

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int maxn=1e5+5;
    char s[maxn];
    int wa[maxn],wb[maxn],wv[maxn],wss[maxn];
    int sa[maxn],ran[maxn],height[maxn];
    
    int cmp(int *r,int a,int b,int l)
    {
        return r[a]==r[b]&&r[a+l]==r[b+l];
    }
    
    void da(char *r,int *sa,int n,int m)
    {
        int i,j,p,*x=wa,*y=wb,*t;
        for(i=0; i<m; i++) wss[i]=0;
        for(i=0; i<n; i++) wss[x[i]=(int)r[i]]++;
        for(i=1; i<m; i++) wss[i]+=wss[i-1];
        for(i=n-1; i>=0; i--) sa[--wss[x[i]]]=i;
        for(j=1,p=1; p<n; j*=2,m=p)
        {
            for(p=0,i=n-j; i<n; i++) y[p++]=i;
            for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
    
            for(i=0; i<n; i++) wv[i]=x[y[i]];
            for(i=0; i<m; i++) wss[i]=0;
            for(i=0; i<n; i++) wss[wv[i]]++;
            for(i=1; i<m; i++) wss[i]+=wss[i-1];
            for(i=n-1; i>=0; i--) sa[--wss[wv[i]]]=y[i];
    
            for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
                x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
        return;
    }
    
    void callheight(char *r,int *sa,int n)
    {
        int i,j,k=0;
        for(i=1;i<=n;i++)
            ran[sa[i]]=i;
        for(i=0;i<n;height[ran[i++]]=k)
        for(k?k--:0,j=sa[ran[i]-1];r[i+k]==r[j+k];k++);
        return ;
    }
    
    int main()
    {
        int T;
        int Case=1;
        cin>>T;
        char x;
        while(T--)
        {
            scanf(" %c",&x);
            scanf("%s",s);
            int len=strlen(s);
            da(s,sa,len+1,130);
            callheight(s,sa,len);
            int nxt[100005];
            int tmp=len;
            long long sum=0;
            for(int i=len-1;i>=0;i--)
            {
                if(s[i]==x) tmp=i;
                nxt[i]=tmp;
            }
            for(int i=1;i<=len;i++)
            {
                sum+=(long long)(len-max(sa[i]+height[i],nxt[sa[i]]));
            }
            printf("Case #%d: %lld
    ",Case++,sum);
        }
        return 0;
    }
  • 相关阅读:
    C++容器不要类型转换
    栈区、堆区、全局区、文字常量区、程序代码区
    C++数据类型字节数
    Exception-Safe Generic Containers
    饭碗是要靠抢的
    Wireshark抓包实例分析TCP重复ACK与乱序
    OSPF
    孩子,我该让你更好的长大!
    云,除了卖,我们还剩下什么?
    动态路由3--链路状态路由选择协议
  • 原文地址:https://www.cnblogs.com/chen9510/p/5743674.html
Copyright © 2020-2023  润新知