hdu 3518 Boring counting 后缀数组
来源:互联网 发布:单片机方案开发 编辑:程序博客网 时间:2024/05/14 20:53
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
字符串长度只有1000 考虑n^2,根据最长不可重叠子串,可以知道 每两个串之间的lcp是递减的,所以暴力跑递减,直到<=height[i-1] 退出,因为height[i-1]都是相当于判断过的,我们只需要判断的是 k-height[i-1]的就可以。
#include <bits/stdc++.h>using namespace std;const int MAXN = 1100;int t1[MAXN],t2[MAXN],c[MAXN];int n;bool cmp(int *r,int a,int b,int l){ return r[a]==r[b]&&r[a+l]==r[b+l];}void da(char str[],int sa[],int ra[],int height[],int n,int m){ n++; int i,j,p,*x=t1,*y=t2; for(i=0;i<m;i++) c[i]=0; for(i=0;i<n;i++) c[x[i]=str[i]]++; for(i=1;i<m;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i; for(j=1;j<=n;j<<=1) { p=0; for(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<m;i++) c[i]=0; for(i=0;i<n;i++) c[x[y[i]]]++; for(i=1;i<m;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; swap(x,y); p=1;x[sa[0]]=0; for(i=1;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; if(p>=n) break; m=p; } int k=0; n--; for(i=0;i<=n;i++) ra[sa[i]]=i; for(i=0;i<n;i++) { if(k) k--; j=sa[ra[i]-1]; while(str[i+k]==str[j+k])k++; height[ra[i]]=k; }}int ra[MAXN],height[MAXN];int sa[MAXN],num[MAXN];typedef long long ll;char str[MAXN];int main(){ while(~scanf("%s",str)) { if(str[0]=='#') break; int n=strlen(str); da(str,sa,ra,height,n,130); ll ans=0; for(int i=1;i<=n;i++) { int k=height[i]; int minn=min(sa[i],sa[i-1]),maxx=max(sa[i],sa[i-1]); int j=i,an=0; while(k&&j<=n) { if(maxx-minn>=k) { ans+=max(k-height[i-1],0); break; } k=min(k,height[j+1]); j++,maxx=max(maxx,sa[j]),minn=min(minn,sa[j]); } } printf("%lld\n",ans ); }}
- 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 3518Boring counting(后缀数组)
- hdu 3518 Boring counting 后缀数组
- Hdu 3518 Boring counting 后缀数组
- HDU 3518 Boring Counting 后缀数组/后缀自动机
- 杭电hdu 3518 boring counting 后缀数组学习
- HDU 3518 Boring counting(后缀数组入门题)
- hdu 3158 Boring counting 后缀数组
- 【后缀数组】 HDOJ 3518 Boring counting
- HDU 3518 Boring counting - 后缀树
- HDU 3518 Boring counting 后缀自动机
- AJAX工作原理及其优缺点
- Hibernate入门六(单项的保存操作)
- 敌兵布阵
- 学习编程,技术那么多,如何选择呢?
- js -- java controller param
- hdu 3518 Boring counting 后缀数组
- Echarts基础(二)柱、饼、折线图
- 用顺序表完成简单的学生成绩管理系统
- spring4.3.x与apach-cxf.2.7x jar冲突
- 检测openstack云平台是否存在脑裂的虚拟机,加入zabbix告警
- 【OpenJudge 1805】碎纸机
- Android四大组件——Service详解(一)
- 线段树 区间更新
- SSM-后端接收前端传递的文件