UVALive 6869 Repeated Substrings(后缀数组)
来源:互联网 发布:海信电视tv软件 编辑:程序博客网 时间:2024/05/16 01:08
String analysis often arises in applications from biology and
chemistry, such as the study of DNA and protein molecules. One
interesting problem is to find how many substrings are repeated
(at least twice) in a long string.
In this problem, you will write a program to find the total
number of repeated substrings in a string of at most 100 000
alphabetic characters. Any unique substring that occurs more
than once is counted. As an example, if the string is “aabaab”,
there are 5 repeated substrings: “a”, “aa”, “aab”, “ab”, “b”.
If the string is “aaaaa”, the repeated substrings are “a”, “aa”, “aaa”, “aaaa”. Note that repeated
occurrences of a substring may overlap (e.g. “aaaa” in the second case).
Input
The first line contains a positive integer, specifying the number of cases to follow. Each of the following
line contains a nonempty string of up to 100 000 alphabetic characters.
Output
For each line of input, output one line containing the number of unique substrings that are repeated.
You may assume that the correct answer fits in a signed 32-bit integer.
Sample Input
3
aabaab
aaaaa
AaAaA
Sample Output
5
4
5
/*后缀数组.题意:求出现过两次以上的不同子串有多少种.字串即字符串后缀的前缀.ans+=ht[i]-ht[i-1].用当前的贡献减去之前已经计算过的贡献.然后把每个前缀的贡献累加.*/#include<iostream>#include<cstring>#include<cstdio>#define MAXN 100010using namespace std;int n,K,m=130,ans,sa[MAXN],rank1[MAXN],c[MAXN],ht[MAXN],t1[MAXN],t2[MAXN],s[MAXN];char ch[MAXN];bool cmp(int *y,int a,int b,int k){ int a1=y[a],b1=y[b]; int a2=a+k>=n?-1:y[a+k]; int b2=b+k>=n?-1:y[b+k]; return a1==b1&&a2==b2;}void slovesa(){ int *x=t1,*y=t2; for(int i=0;i<m;i++) c[i]=0; for(int i=0;i<n;i++) c[x[i]=s[i]]++; for(int i=1;i<m;i++) c[i]+=c[i-1]; for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i; for(int k=1,p=0;k<=n;k<<=1,m=p,p=0) { for(int i=n-k;i<n;i++) y[p++]=i; for(int i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k; for(int i=0;i<m;i++) c[i]=0; for(int i=0;i<n;i++) c[x[y[i]]]++; for(int i=1;i<m;i++) c[i]+=c[i-1]; for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; swap(x,y),p=1,x[sa[0]]=0; for(int i=1;i<n;i++) { if(cmp(y,sa[i-1],sa[i],k)) x[sa[i]]=p-1; else x[sa[i]]=p++; } if(p>=n) break; }}void sloveheight(){ int k=0; for(int i=0;i<n;i++) rank1[sa[i]]=i; for(int i=0;i<n;ht[rank1[i++]]=k) { int j=sa[rank1[i]-1]; if(k) k--; while(j+k<n&&i+k<n&&s[i+k]==s[j+k]) k++; } ht[0]=0;}void slove(){ for(int i=1;i<n;i++) { int l=n-sa[i-1]-1; if(ht[i]>=ht[i-1]) ans+=ht[i]-ht[i-1]; } printf("%d\n",ans);}void Clear(){ ans=0,m=130; memset(sa,0,sizeof sa);}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%s",ch);Clear(); n=strlen(ch); for(int i=0;i<n;i++) s[i]=ch[i]; s[n++]=0; slovesa(),sloveheight(),slove(); } return 0;}
- UVALive 6869 Repeated Substrings(后缀数组)
- CSU1632: Repeated Substrings(后缀数组)
- CSU 1632 Repeated Substrings(后缀数组)
- poj1226 Substrings 后缀数组
- Distinct Substrings后缀数组
- POJ1226:Substrings(后缀数组)
- Distinct Substrings 后缀数组
- POJ_3415 Common Substrings 后缀数组
- POJ 1226 Substrings(后缀数组)
- spoj694 Distinct Substrings 后缀数组
- POJ 1226 Substrings (后缀数组)
- 【POJ 1226】Substrings【后缀数组】
- POJ 1226 Substrings 后缀数组
- POJ 1226 Substrings (后缀数组)
- SPOJ Distinct Substrings 后缀数组
- Substrings poj1226 (后缀数组)
- HDU2559-Longest Repeated subsequence(后缀数组)
- SPOJ 694. Distinct Substrings,705. New Distinct Substrings(后缀数组)
- 关于iOS开发
- 如何规避GRADLE每次都要下载的问题
- Stanford机器学习---第八讲. 支持向量机SVM
- MVP模式
- Firebug指令大全
- UVALive 6869 Repeated Substrings(后缀数组)
- tensorflow学习笔记十7:tensorflow官方文档学习 How to Retrain Inception's Final Layer for New Categories
- 编程二
- 实例方法
- 大数据和高并发的解决方案总结
- Memcache
- 对Linux新手非常有用的 20个命令
- L2-014. 列车调度
- 单链表翻转