有多少个不同的子串?-- 后缀数组
2016-09-28 14:08
281 查看
点击打开题目链接
题目大意: 给你一个字符串问你有多少个不相同的子串?
T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 50000
For each test case output one number saying the number of distinct substrings.
题目思路:利用后缀数组这个工具,不会后缀数组详见 后缀数组论文链接——处理字符串的有力工具_百度文库。这篇文章说的比较详细,思路就是一共会有
n*(n+1)/2 减去重复的就可以了,
举个栗子. ABABA 比如这个.他的后缀排序之后是:
• A -------------------后缀 a
• ABA ---------------后缀 b
• ABABA ----------后缀 c
• BA -----------------后缀 d
• BABA -------------后缀 e
这样,重复的子串就是 (a,b)的最长公共前缀长度
答案就是 15 - { lcp(a,b)=1 } - { lcp(b,c)=3 } - { lcp(c,d)=0 } - { lcp(d,e)=2 }
=15-1-3-0-2=9;
下面是模板代码
题目大意: 给你一个字符串问你有多少个不相同的子串?
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
题目思路:利用后缀数组这个工具,不会后缀数组详见 后缀数组论文链接——处理字符串的有力工具_百度文库。这篇文章说的比较详细,思路就是一共会有
n*(n+1)/2 减去重复的就可以了,
举个栗子. ABABA 比如这个.他的后缀排序之后是:
• A -------------------后缀 a
• ABA ---------------后缀 b
• ABABA ----------后缀 c
• BA -----------------后缀 d
• BABA -------------后缀 e
这样,重复的子串就是 (a,b)的最长公共前缀长度
答案就是 15 - { lcp(a,b)=1 } - { lcp(b,c)=3 } - { lcp(c,d)=0 } - { lcp(d,e)=2 }
=15-1-3-0-2=9;
下面是模板代码
#include<cstdio> #include<cstring> #include<cctype> #include<cmath> #include<set> #include<map> #include<list> #include<queue> #include<deque> #include<stack> #include<string> #include<vector> #include<iostream> #include<algorithm> #include<stdlib.h> #include<time.h> using namespace std; typedef long long LL; const int INF=2e9+1e8; const int MOD=1e9+7; const int MAXSIZE=1e6+5; const double eps=0.0000000001; void fre() { freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); } #define memst(a,b) memset(a,b,sizeof(a)) #define fr(i,a,n) for(int i=a;i<n;i++) int rankarr[MAXSIZE],wa[MAXSIZE],wb[MAXSIZE],height[MAXSIZE]; int wvarr[MAXSIZE],wsarr[MAXSIZE],SA[MAXSIZE]; char str[MAXSIZE]; int cmp(int *r,int a,int b,int l) { return r[a]==r[b]&&r[a+l]==r[b+l]; } void da(char *r,int *sa,int n,int m) { int i,j,p,*x=wa,*y=wb,*t; for(i=0; i<m; i++) wsarr[i]=0; for(i=0; i<n; i++) wsarr[x[i]=r[i]]++; for(i=1; i<m; i++) wsarr[i]+=wsarr[i-1]; for(i=n-1; i>=0; i--) sa[--wsarr[x[i]]]=i; for(j=1,p=1; p<n; j<<=1,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++) wvarr[i]=x[y[i]]; for(i=0; i<m; i++) wsarr[i]=0; for(i=0; i<n; i++) wsarr[wvarr[i]]++; for(i=1; i<m; i++) wsarr[i]+=wsarr[i-1]; for(i=n-1; i>=0; i--) sa[--wsarr[wvarr[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++; } return; } void calheight(char *r,int *sa,int n) { int i,j,k=0; for(i=1; i<=n; i++) rankarr[sa[i]]=i; for(i=0; i<n; height[rankarr[i++]]=k) for(k?k--:0,j=sa[rankarr[i]-1]; r[i+k]==r[j+k]; k++); return; } LL solve(int n) { LL ans=n; for(int i=2;i<=n;i++) ans+=n-i+1-height[i]; return ans; } int main() { int ncase; cin>>ncase; while(ncase--) { scanf("%s",str); int n=strlen(str); str =0; da(str,SA,n+1,128); calheight(str,SA,n); cout<<solve(n)<<endl; } return 0; }
相关文章推荐
- 有多少个不同的子串?-- 后缀数组
- spoj 705 求不同子串的个数(后缀数组)
- [TyvjP1515] 子串统计 [luoguP2408] 不同子串个数(后缀数组)
- SPOJ DISUBSTR - Distinct Substrings(不同子串数量 后缀数组)
- spoj 405 求不同子串的个数 后缀数组和高度数组的应用
- SPOJ-694-求字符串中不同子串个数(后缀数组)
- SPOJ 694 求不同子串数 后缀数组
- SPOJ - DISUBSTR 多少个不同的子串
- spoj 694 Distinct Substrings(求不同的子串个数,后缀数组基础题)
- hdu3948——后缀数组统计不同回文子串的个数
- SPOJ - DISUBSTR 多少个不同的子串
- Distinct Substrings 给出一个字符串,问它的不重复子串有多少个 后缀数组
- Distinct Substrings SPOJ - DISUBSTR 后缀数组(计算不同的子串数目)
- spoj694 Distinct Substrings(后缀数组+统计不同子串的个数)
- CSU 1632 最长公共前缀(出现超过两次的不同子串有多少个)
- New Distinct Substrings (后缀数组,统计有多少个不同的子串)
- hdu 4416 后缀自动机 问在S中有多少个不同子串满足它不是s1~sn中任意一个字符串的子串
- 第39级台阶 每一步只能迈上1个或2个台阶.先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步.那么,上完39级台阶,有多少种不同的上法(java代码)
- 1.写一个函数返回参数二进制中 1 的个数;2..获取一个数二进制序列中所有的偶数位和奇数位;3. 输出一个整数的每一位;4.两个int整数的二进制表达中,有多少个位不同
- hdu 3065 病毒侵袭持续中 DFA 求字符串最多匹配多少字符串 每个子串最多匹配多少个