太阳不下山 发表于 2021-7-21 11:05:01

hdu 5769 Substring 后缀数组

Substring

题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5769

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.

Hint

题意
给你个字符串,问你至少含有一个x字符的子串数量有多少个

题解:
处理出后缀数组中的sa[]数组和height[]数组。在不考虑包含字符X的情况下,不同子串的个数为\(\sum_{1}^{length} length-(sa+height)\)
如果要求字符X,只需要记录距离sa最近的字符X的位置(用nxt表示)即可,个数为\(\sum_{1}^{length} length-max(nxt,sa+height)\)

代码#include <bits/stdc++.h>

using namespace std;

const int maxn=100100;
const int inf=0x3f3f3f3f;
int wa,wb,wn,wv;
int rk,height,sa,r,n;
int last;
char str,ss;

int cmp(int *r,int a,int b,int l)
{
    return (r==r)&&(r==r);
}
void da(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0;i<m;i++) wn=0;
    for(i=0;i<n;i++) wn=r]++;
    for(i=1;i<m;i++) wn+=wn;
    for(i=n-1;i>=0;i--) sa[--wn]]=i;
    for(j=1,p=1;p<n;j*=2,m=p)
    {
      for(p=0,i=n-j;i<n;i++) y=i;
      for(i=0;i<n;i++) if(sa>=j) y=sa-j;
      for(i=0;i<n;i++) wv=x];
      for(i=0;i<m;i++) wn=0;
      for(i=0;i<n;i++) wn]++;
      for(i=1;i<m;i++) wn+=wn;
      for(i=n-1;i>=0;i--) sa[--wn]]=y;
      for(t=x,x=y,y=t,p=1,x]=0,i=1;i<n;i++)
            x]=cmp(y,sa,sa,j)?p-1:p++;
    }
}
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1;i<=n;i++) rk]=i;
    for(i=0;i<n;height]=k )
    for(k?k--:0,j=sa-1];r==r;k++);
}

int main()
{
    //freopen("1.in","r",stdin);
    int T;
    scanf("%d",&T);
    for(int o=1;o<=T;o++)
    {
      scanf("%s",ss);
      scanf("%s",str);
      int n=strlen(str),now=n;
      for(int i=n-1;i>=0;i--)
      {
            if(str==ss) now=i;
            last=now;
      }
      for(int i=0;i<n;i++)
            r=str;
      r=0;
      da(r,sa,n+1,256);
      calheight(r,sa,n);
      long long ans=0;
      for(int i=1;i<=n;i++)
      {
            int tmp=max(last]-sa,height);
            ans+=1LL*max(0,n-sa-tmp);
      }
      printf("Case #%d: ",o);
      cout<<ans<<endl;
    }
    return 0;
}
文档来源:51CTO技术博客https://blog.51cto.com/u_15303184/3108595
页: [1]
查看完整版本: hdu 5769 Substring 后缀数组