2016多校训练Contest4: 1006 Substring hdu5769

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.

果然字符串还是要去学一下。。这个签到题直接因为不会SA和SAM所以没出。。

单个后缀贡献为len-max(next[sa[i]],sa[i]+height[i])

next[i]表示i之后的第一个字母X

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
string s;
int len;
int rk[100011],sa[100011];
//rk 当前位置排名
//sa 排名第i的位置 
int wx[100011],wy[100011],c[100011];
inline bool cmp(int *s,int a,int b,int l){return s[a]==s[b]&&s[a+l]==s[b+l];}
inline void build(int n,int m)
{
	memset(sa,0,sizeof(sa));
	memset(rk,0,sizeof(rk));
	memset(wx,0,sizeof(wx));
	memset(wy,0,sizeof(wy));
	memset(c,0,sizeof(c));
	int i;
	int *x=wx,*y=wy,*t;
	int p;
	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[s[i]]]=i;
	int k=1;
	while(k<len)
	{
		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];
		t=x;x=y;y=t;
		x[sa[0]]=rk[sa[0]]=0;
		p=1;
		for(i=1;i<n;i++)
        {  
            if(k*2<n)  
                x[sa[i]]=cmp(y,sa[i-1],sa[i],k)?p-1:p++;  
        } 
		k=k*2;
		m=p;
	}
	for(i=1;i<=n-1;i++)	rk[sa[i]]=i;
}
int height[100001],h[100001];
//height 相邻后缀的最长公共前缀 i与i-1 
//h 排名相邻后缀的最长公共前缀 i与i-1 
inline void findh()
{
	memset(height,0,sizeof(height)); 
	int i,j,k=0;
	for(i=0;i<len;i++)
	{
		j=sa[rk[i]-1];
		if(k>0)
			k--;
		while(s[i+k]==s[j+k])
			k++;
		height[rk[i]]=k;
	}
}
int f[100005];
int main()
{
//	freopen("1006.in","r",stdin);
//	freopen("1006.ans","w",stdout);
	int k=0;
	int T;
	scanf("%d",&T);
	while(T>0)
	{
		T--;
		k++;
		string x1;
		cin>>x1>>s;
		len=s.size();
		s[len]=0;
		build(len+1,128);
		findh();
		int i;
		memset(f,0,sizeof(f));
		f[len]=len;
		for(i=len-1;i>=0;i--)
		{
			if(s[i]==x1[0])
				f[i]=i;
			else
				f[i]=f[i+1];
		}
		long long ans=0;
		for(i=1;i<=len;i++)
			ans+=(long long)(len-max(f[sa[i]],sa[i]+height[i]));
		printf("Case #%d: %I64d\n",k,ans);
	}
	return 0; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值