HDU 3518 Boring counting(后缀数组入门题)
2016-02-27 15:16
127 查看
题目链接:点击打开链接
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2617 Accepted Submission(s): 1062
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”).
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
Sample Output
题意:求至少出现两次的不重叠子串的数目总和。后缀数组入门,但是我不会告诉你我wa了多少次。注意的地方写在代码里。
AC代码:
Boring counting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2617 Accepted Submission(s): 1062
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”).
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
aaaa ababcabb aaaaaa #
Sample Output
2 3 3
题意:求至少出现两次的不重叠子串的数目总和。后缀数组入门,但是我不会告诉你我wa了多少次。注意的地方写在代码里。
AC代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> using namespace std; int wa[200005],wb[200005],wv[200005],ws[200005]; int r[200005],height[200005],rank1[200005],sa[200005]; 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,*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++; } return; } void calheight(int *r,int *sa,int n){ int i,j,k=0; for(i=1;i<=n;i++) rank1[sa[i]]=i; for(i=0;i<n;height[rank1[i++]]=k) for(k?k--:0,j=sa[rank1[i]-1];r[i+k]==r[j+k];k++); return; } int main() { int i,j,len;char s[1100]; while(scanf("%s",s)!=EOF&&s[0]!='#') { len=strlen(s); for(i=0;i<len;i++) r[i]=s[i]; r[len]=0; da(r,sa,len+1,300); calheight(r,sa,len); int Max,Min,ans=0; for(i=1;i<=len;i++) { Max=sa[1],Min=sa[1]; for(j=2;j<=len;j++) { if(height[j]>=i) { Max=max(Max,max(sa[j-1],sa[j])); Min=min(Min,min(sa[j-1],sa[j])); } else { if(Max-Min>=i) ans++; Max=sa[j],Min=sa[j]; } } if(Max-Min>=i) ans++;//可能从来没有进入过else里面,因此要加一个判断。 } printf("%d\n",ans); } }
相关文章推荐
- python简单的绘制折现图
- C json实战引擎 一 , 实现解析部分
- 二分法的学习
- 深入理解JVM之垃圾回收详解
- Java web的web-app 的web.xml 设置
- unlocked_ioctl和compat_ioctl
- ViewPager+小圆点进行无限轮播
- widows 下git记住账号密码
- [leetcode Q28] Implement strStr()
- 关于ViewPager
- 初学 Java Web 开发,请远离各种框架,从 Servlet 开发
- 百度地图报错 uncaught exception (group=0x416f2e00)
- 千里码【65】可视化_1
- 【rabbitmq】解决SimpleAmqpClient创建连接时阻塞的问题
- 基于 Apache 在本地配置多个虚拟主机
- 边缘检测
- HDU 5294 Tricks Device(最短路+最大流)
- 在Eclipse中使用JUnit4进行单元测试(高级篇)
- 第三届_银行密码
- MVVM设计模式具体实现