HDU5769 Substring 后缀数组

题目链接:HDU5769

Substring

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


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.
 

题意:求母串中包含特定字符的不同子串的个数。

题目分析:显然是后缀数组的题,由于需要在子串中包含特定字符,所以我们可以先记录哪些后缀有这些字符以及最远出现的位置。之后统计每个后缀能够贡献的子串数,正常情况下子串数为len-sa[i]-height[i],但是考虑字符的位置pos,所以子串数为len-max((sa[i]+height[i]),pos)。

//
//  main.cpp
//  HDU5769
//
//  Created by teddywang on 2016/9/22.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100010;
int t1[maxn],t2[maxn],c[maxn];
bool cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int str[],int sa[],int ranks[],int height[],int n,int m)
{
    n++;
    int i,j,p,*x=t1,*y=t2;
    for(i=0;i<m;i++) c[i]=0;
    for(i=0;i<n;i++) c[x[i]=str[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;
        for(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<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];
        swap(x,y);
        p=1;x[sa[0]]=0;
        for(i=1;i<n;i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        if(p>=n) break;
        m=p;
    }
    int k=0;
    n--;
    for(i=0;i<=n;i++)ranks[sa[i]]=i;
    for(i=0;i<n;i++)
    {
        if(k)k--;
        j=sa[ranks[i]-1];
        while(str[i+k]==str[j+k])k++;
        height[ranks[i]]=k;
    }
}
int ranks[maxn],height[maxn];
char str[maxn],s1[maxn];
int r[maxn],sa[maxn];
int pre[maxn],vis[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        int flag=0,fpre=0;
        scanf("%s%s",s1,str);
        int len=strlen(str);
        int n=len;
        for(int i=0;i<=len;i++) r[i]=str[i];
        r[len]=0;
        memset(vis,0,sizeof(vis));
        memset(pre,0,sizeof(pre));
        for(int i=len-1;i>=0;i--)
        {
            if(str[i]==s1[0])
            {
                flag=1;fpre=i;
            }
            if(flag==1)
            {
                vis[i]=1;
                pre[i]=fpre;
            }
        }
        long long int ans=0;
        da(r,sa,ranks,height,n,128);
        for(int i=0;i<=n;i++)
        {
            if(vis[sa[i]]) ans=ans+len-max((sa[i]+height[i]),pre[sa[i]]);
        }
        printf("Case #%d: %lld\n",cas,ans);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值