HDU 3518 Boring counting 重复出现不重叠子串个数(后缀数组)
来源:互联网 发布:mac 替换war文件 编辑:程序博客网 时间:2024/06/01 10:36
点击打开链接
Boring counting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1417 Accepted Submission(s): 581
Problem Description
035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other.
Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from [0-2](The position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).
Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from [0-2](The position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).
Input
The input data consist with several test cases.The input ends with a line “#”.each test case contain a string consists with lower letter,the length n won’t exceed 1000(n <= 1000).
Output
For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.
Sample Input
aaaaababcabbaaaaaa#
Sample Output
233
Source
2010 ACM-ICPC Multi-University Training Contest(9)——Host by HNU
给你一个字符串,让你求至少出现两次且不重叠的子串的个数。
分别求出后缀数组的sa,rank和height数组,然后枚举子串的长度,记录每组中最小和最大后缀数组的起始位置minn和maxx,并且判断maxx-minn>=k,如果是那么结果+1.
//62MS284K#include<stdio.h>#include<string.h>#include<algorithm>#define M 500007#define inf 0x3f3f3fusing namespace std;int sa[M],rank[M],height[M];int wa[M],wb[M],wv[M],ws[M];char str[1007];int num[1007];int cmp(int *r,int a,int b,int l){ return r[a]==r[b]&&r[a+l]==r[b+l];}void get_sa(int *r,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++; }}void get_height(int *r,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++);}int solve(int k,int n){ int maxx=0,minn=inf,ans=0; for(int i=1;i<=n;i++) if(height[i]<k) { if(maxx-minn>=k) { ans++; //printf("%d %d\n",maxx,minn); } maxx=0,minn=inf; } else { maxx=max(max(sa[i-1],sa[i]),maxx); minn=min(min(sa[i-1],sa[i]),minn); } if(maxx-minn>=k)ans++; return ans;}int main(){ while(scanf("%s",str),str[0]!='#') { int n=strlen(str); for(int i=0;i<n;i++) num[i]=str[i]-'a'+1; num[n]=0; int ans=0,m=30; get_sa(num,n+1,m); get_height(num,n); //for(int i=0;i<=n;i++) // printf("i==%d,sa==%d,height==%d\n",i,sa[i],height[i]); for(int i=1;i<=n/2;i++) ans+=solve(i,n); printf("%d\n",ans); } return 0;}
0 0
- HDU 3518 Boring counting 重复出现不重叠子串个数(后缀数组)
- HDU 3518 Boring counting(后缀数组啊 求字符串中不重叠的重复出现至少两次的子串的个数)
- HDU 3518 Boring counting(后缀数组 所有不重叠字串出现两次以上的次数)
- Hdu 3518 Boring counting (出现2次及以上且不重叠的子串的个数)
- HDOJ 题目3518 Boring counting(后缀数组,求不重叠重复次数最少为2的子串种类数)
- hdu 3518 ( 后缀数组 至少出现两次的不重叠子串个数)
- hdu 3518 Boring counting(后缀数组)
- HDU 3518Boring counting(后缀数组)
- HDU 3518 Boring counting(后缀数组)
- HDU 3518 Boring counting && 后缀数组
- [后缀数组+枚举] hdu 3518 Boring counting
- 【HDU】3518 Boring counting 后缀数组
- hdu 3518 Boring counting 后缀数组
- HDU 3518 Boring counting(后缀数组)
- 后缀数组 --- HDU 3518 Boring counting
- hdu 3518 Boring counting 后缀数组
- Hdu 3518 Boring counting 后缀数组
- HDU 3518 Boring counting(后缀数组入门题)
- 一小时内破解16位强密码
- Xcode 编程中得常用快捷键
- win7中使用runas命令(替代右键“以管理员身份运行”)
- Java 中使用HttpURLConnection发起POST 请求
- 纳德拉干得不错!微软股价逼近14年最高点
- HDU 3518 Boring counting 重复出现不重叠子串个数(后缀数组)
- 在Eclipse中使用JUnit4进行单元测试
- Twitter野心:收购两家欧洲电视数据分析公司
- Android 常用控件讲解
- java中main函数解析
- C++编程->异常处理入门
- POJ 2336 Ferry Loading II (贪心或动态规划)
- 安装StatTransfer过程的错误libstdc++.so.5解决
- 《深入浅出WPF》笔记——绘画与动画