HDU 5769-Substring(字尾陣列-不相同的子串的個數)
Substring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 973 Accepted Submission(s): 393
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: 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
Recommend
題目意思:
有一個子串X,一個母串str,求str中不相同的子串個數且至少含有一個X串。
解題思路:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define maxn 1000010
using namespace std;
//以下為倍增演算法求字尾陣列
int wa[maxn],wb[maxn],wv[maxn],Ws[maxn],nxt[maxn];
int sa[maxn],Rank[maxn],height[maxn];
char str[maxn],X[maxn];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(const char *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0; i<m; i++) Ws[i]=0;
for(i=0; i<n; i++) Ws[x[i]=r[i]]++;
for(i=1; i<m; i++) Ws[i]+=Ws[i-1];
for(i=n-1; i>=0; i--) sa[--Ws[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++) Ws[i]=0;
for(i=0; i<n; i++) Ws[wv[i]]++;
for(i=1; i<m; i++) Ws[i]+=Ws[i-1];
for(i=n-1; i>=0; i--) sa[--Ws[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++;
}
}
//求height陣列
void calheight(const char *r,int *sa,int n)
{
int i,j,k=0;
for(i=1; i<=n; i++) Rank[sa[i]]=i;
for(i=0; i<n; height[Rank[i++]]=k)
for(k?k--:0,j=sa[Rank[i]-1]; r[i+k]==r[j+k]; k++);
return;
}
int slove(int n)
{
int sum=0;
for(int i=1; i<=n; i++)
sum+=n-sa[i]-height[i];
return sum;
}
int main()
{
int t,ca=0;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",X,str);
int n=strlen(str);
int temp=n;
long long ans=0;
for(int i=n-1; i>=0; i--)
{
if(str[i]==X[0])
{
temp=i; cout<<temp<<"===";
}
else cout<<temp<<"***";
nxt[i]=temp;
}
da(str,sa,strlen(str)+1,130);
calheight(str,sa,strlen(str));
for(int i=1; i<=n; i++)
// printf("%d %d\n",sa[i],height[i]);
// printf("%d\n",slove(strlen(str)));
ans+=n-max(nxt[sa[i]],sa[i]+height[i]);
printf("Case #%d: %I64d\n",++ca,ans);
}
return 0;
}
/**
2
a
abc
b
bbb
**/
相關文章
- POJ 3294 Life Forms(字尾陣列求k個串的最長子串)ORM陣列
- HDU 4622 Reincarnation( 任意區間子串的長度, 字尾陣列+RMQ)陣列MQ
- poj 2774 求兩字串的最長公共子串 字尾陣列字串陣列
- ural 1297 最長迴文子串 字尾陣列陣列
- POJ 2217-Secretary(字尾陣列+高度陣列-最大公共子串長度)陣列
- SPOJ 687. Repeats(字尾陣列求最長重複子串)陣列
- SPOJ 694 求一個字串有多少子串 字尾陣列字串陣列
- POJ 3693 Maximum repetition substring(字尾陣列求最長重複子串)陣列
- SPOJ 220. Relevant Phrases of Annihilation(字尾陣列多次不重疊子串)陣列
- POJ 2774-Long Long Message(字尾陣列+高度陣列-最大公共子串長度)陣列
- POJ 1743 Musical Theme (字尾陣列,求最長不重疊重複子串)陣列
- POJ 3415-Common Substrings(字尾陣列+單調棧-公共子串的長度)陣列
- 字尾陣列模板陣列
- 字尾陣列 SA陣列
- 字尾陣列,SA陣列
- 字尾陣列複習陣列
- 字尾陣列(後續)陣列
- 字尾陣列詳解陣列
- 【筆記】字尾陣列筆記陣列
- URAL 1297. Palindrome(字尾陣列求最大回文串)陣列
- HDU5147 Sequence II(樹狀陣列+字首和+字尾和)陣列
- DreamJudge-1294-字尾子串排序排序
- poj 3415 Common Substrings(長度大於k的相同子串對數xian 字尾陣列+單調桟統計)陣列
- OI loves Algorithm——字尾陣列Go陣列
- 學習筆記----字尾陣列筆記陣列
- 字尾陣列 學習筆記陣列筆記
- 字尾陣列學習筆記陣列筆記
- 演算法學習-數字連續的子陣列演算法陣列
- 【Kmp求既是字首又是字尾的子串】POJ - 2752 Seek the Name, Seek the FameKMP
- BZOJ2882: 工藝(字尾陣列)陣列
- POJ 3581-Sequence(字尾陣列)陣列
- POJ 1743 Musical Theme(字尾陣列)陣列
- C# 輸出一個字串的字首、字尾和它的子串(資訊內容安全 實驗一)C#字串
- hdu4417 樹狀陣列(求指定區間比指定數小的數的個數)陣列
- 計算陣列中每個數字出現的次數陣列
- 陣列中重複的數字陣列
- ES6字串轉換為陣列字串陣列
- 數字之魅:子陣列之和的最大值[二維]+[三維]陣列