SPOJ 694. Distinct Substrings,705. New Distinct Substrings(字尾陣列)
題目大意:給定長度為N的字串,求出其中不相同子串的個數。
解題思路:每一個字串一定是某個字尾的字首,那麼原問題就可以等價於求所有字尾之間的不相同的字首的個數。如果所有的字尾按照suffix(sa[1]),suffix(sa[2])……suffix(sa[n])的順序計算,我們會發現對於每個新加進來的字尾suffix(sa[k]),它將產生n-sa[k]+1個新的字首。但是其中有leight[k]個是和前面的字串的字首是相同的。所以suffix(sa[k])加進來增加的不同的子串的個數為n-sa[k]+1-height[k]。累加起來就是所有字串的數目了。
705. New Distinct Substrings
Problem code: SUBST1
Given a string, we need to find the total number of its distinct substrings.
Input
T- number of test cases. T<=20;Each test case consists of one string, whose length is <= 50000
Output
For each test case output one number saying the number of distinct substrings.
Example
Input: 2 CCCCC ABABA Output: 5 9
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007
#define Read() freopen("autocomplete.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
#define Cin() ios::sync_with_stdio(false)
using namespace std;
inline int read()
{
char ch;
bool flag = false;
int a = 0;
while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));
if(ch != '-')
{
a *= 10;
a += ch - '0';
}
else
{
flag = true;
}
while(((ch = getchar()) >= '0') && (ch <= '9'))
{
a *= 10;
a += ch - '0';
}
if(flag)
{
a = -a;
}
return a;
}
void write(int a)
{
if(a < 0)
{
putchar('-');
a = -a;
}
if(a >= 10)
{
write(a / 10);
}
putchar(a % 10 + '0');
}
const int maxn = 50010;
int wa[maxn], wb[maxn], wv[maxn], ws1[maxn];
int sa[maxn];
int cmp(int *r, int a, int b, int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
int i, j, p, *x = wa,*y = wb;
for(i = 0; i<m; i++) ws1[i]=0;
for(i = 0; i<n; i++) ws1[x[i]=r[i]]++;
for(i = 1; i<m; i++) ws1[i]+=ws1[i-1];
for(i = n-1; i>=0; i--) sa[--ws1[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++) ws1[i]=0;
for(i = 0; i<n; i++) ws1[wv[i]]++;
for(i = 1; i<m; i++) ws1[i]+=ws1[i-1];
for(i = n-1; i>=0; i--) sa[--ws1[wv[i]]]=y[i];
for(swap(x,y),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++;
}
return;
}
int rank[maxn],height[maxn];
void calheight(int *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 sqe[maxn];
bool judge(int mid, int n)
{
int Max, Min;
for(int i = 2; i <= n; i++)
{
Max = sa[i-1];
Min = sa[i-1];
while(height[i] >= mid)
{
Max = max(sa[i], Max);
Min = min(sa[i], Min);
i++;
}
if(Max - Min > mid) return true;
}
return false;
}
char str[maxn];
int main()
{
///Cin();
int T;
cin >>T;
while(T--)
{
cin >>str;
int len = strlen(str);
for(int i = 0; i < len; i++)
sqe[i] = str[i];
sqe[len] = 0;
da(sqe, sa, len+1, 200);
calheight(sqe, sa, len);
int ans = 0;
for(int i = 0; i < len; i++)
ans += len-i-height[rank[i]];
cout<<ans<<endl;
}
return 0;
}
相關文章
- POJ 3415 Common Substrings(字尾陣列求重複字串)陣列字串
- [POJ 3415] Common Substrings (字尾陣列+單調棧優化)陣列優化
- 【Leetcode】1180. Count Substrings with Only One Distinct LetterLeetCode
- POJ 3415-Common Substrings(字尾陣列+單調棧-公共子串的長度)陣列
- SPOJ 694 求一個字串有多少子串 字尾陣列字串陣列
- SPOJ 687. Repeats(字尾陣列求最長重複子串)陣列
- poj 3415 Common Substrings(長度大於k的相同子串對數xian 字尾陣列+單調桟統計)陣列
- SPOJ 220. Relevant Phrases of Annihilation(字尾陣列多次不重疊子串)陣列
- 字尾陣列模板陣列
- 字尾陣列 SA陣列
- 字尾陣列,SA陣列
- Swift 中 Substrings 與 StringSwift
- 字尾陣列複習陣列
- 字尾陣列(後續)陣列
- 字尾陣列詳解陣列
- 【筆記】字尾陣列筆記陣列
- distinct 多列問題 group by 解決
- Subarray Distinct Values
- HDU 4455 Substrings(預處理+dp)
- OI loves Algorithm——字尾陣列Go陣列
- RxJava_distinct&distinctUntilChangedRxJava
- 學習筆記----字尾陣列筆記陣列
- 字尾陣列 學習筆記陣列筆記
- 字尾陣列學習筆記陣列筆記
- Leetcode Distinct SubsequencesLeetCode
- oracle Distinct|Unique 異同Oracle
- BZOJ2882: 工藝(字尾陣列)陣列
- POJ 3581-Sequence(字尾陣列)陣列
- POJ 1743 Musical Theme(字尾陣列)陣列
- Oracle vs PostgreSQL Develop(15) - DISTINCT ONOracleSQLdev
- Leetcode-Distinct SubsequencesLeetCode
- Distinct Subsequences leetcode javaLeetCodeJava
- DISTINCT和GROUP BY的區別
- SQL Server中distinct的用法SQLServer
- count_sum_distinct與nullNull
- uniq(uid) distinct uidUI
- cf914F. Substrings in a String(bitset 字串匹配)字串匹配
- PostgreSQL 資料庫中 DISTINCT 關鍵字的 4 種用法SQL資料庫