Substring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 390 Accepted Submission(s): 163
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.
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.
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: 3HintIn 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
处理出后缀数组中的sa[]数组和height[]数组。在不考虑包含字符X的情况下,不同子串的个数为∑1lengthlength−(sa[i]+height[i])
如果要求字符X,只需要记录距离sa[i]最近的字符X的位置(用nxt[sa[i]]表示)即可,个数为∑1lengthlength−max(nxt[sa[i]],sa[i]+height[i])
/* ***********************************************
Author : ryc
Created Time : 2016-07-29 Friday
File Name : E:\acmcode\hdoj\5769.cpp
LANGUAGE : c++
Copyright 2016 ryc All Rights Reserved
************************************************ */
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int maxn = 100010;
char s[maxn];
int sa[maxn],t1[maxn],t2[maxn],c[maxn];
int Rank[maxn],height[maxn];
int pos[maxn];
void getHeight(int n){
int k = 0;
for(int i=1;i<=n;i++)Rank[sa[i]] = i;
for(int 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;
}
}
bool cmp(int *r,int a,int b,int l){
return (r[a]==r[b] && r[a+l]==r[b+l]);
}
void build_sa(int m,int n){
int i,*x=t1,*y=t2,k,p;
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(k=1,p=0;p<n;m=p,k<<=1){
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=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],k)?p-1:p++;
}
getHeight(n-1);
}
long long solve(int n){
long long ans = n - sa[1];
if(pos[sa[1]]-sa[1]>n){
ans=0;
}
else {
ans=n-sa[1]-(pos[sa[1]]-sa[1]);
}
for(int i=2;i<=n;i++){
if(pos[sa[i]]-sa[i]<height[i]){
ans += n-sa[i]-height[i];
}
else {
ans += max(n-sa[i]-(pos[sa[i]]-sa[i]),0);
}
}
return ans;
}
int main()
{
int T,test=1;
cin>>T;
while(T--){
char xs[3];
scanf("%s",xs);
scanf("%s",s);
int n = strlen(s);
for(int i=0;i<1e5+10;++i){
pos[i]=1e5+10;
}
for(int i=0;i<n;++i){
if(i==0){
for(int j=i;j<n;++j){
if(s[j]==xs[0]){
pos[i]=j;break;
}
}
}
else if(i<=pos[i-1]){
pos[i]=pos[i-1];
}
else {
for(int j=i;j<n;++j){
if(s[j]==xs[0]){
pos[i]=j;break;
}
}
}
}
build_sa(255,n+1);
printf("Case #%d: %lld\n",test++,solve(n));
}
return 0;
}