SPOJ题目687 Repeats(后缀数组+RMQ求重复次数最多的子串的重复次数)
来源:互联网 发布:程序员教程 第4版下载 编辑:程序博客网 时间:2024/04/29 20:03
REPEATS - Repeats
A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string
s = abaabaabaaba
is a (4,3)-repeat with t = aba as its seed string. That is, the seed string t is 3 characters long, and the whole string s is obtained by repeating t 4 times.
Write a program for the following task: Your program is given a long string u consisting of characters ‘a’ and/or ‘b’ as input. Your program must find some (k,l)-repeat that occurs as substring within u with k as large as possible. For example, the input string
u = babbabaabaabaabab
contains the underlined (4,3)-repeat s starting at position 5. Since u contains no other contiguous substring with more than 4 repeats, your program must output the maximum k.
Input
In the first line of the input contains H- the number of test cases (H <= 20). H test cases follow. First line of each test cases is n - length of the input string (n <= 50000), The next n lines contain the input string, one character (either ‘a’ or ‘b’) per line, in order.
Output
For each test cases, you should write exactly one interger k in a line - the repeat count that is maximized.
Example
Input:117babbabaabaabaababOutput:4since a (4, 3)-repeat is found starting at the 5th character of the input string.
ac代码
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #define min(a,b) (a>b?b:a) #define max(a,b) (a>b?a:b) using namespace std; char str[53030]; int sa[53030],Rank[53030],rank2[53030],height[53030],c[53030],*x,*y,s[53030]; int n; void cmp(int n,int sz) { int i; memset(c,0,sizeof(c)); for(i=0;i<n;i++) c[x[y[i]]]++; for(i=1;i<sz;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; } void build_sa(char *s,int n,int sz) { x=Rank,y=rank2; int i,j; for(i=0;i<n;i++) x[i]=s[i],y[i]=i; cmp(n,sz); int len; for(len=1;len<n;len<<=1) { int yid=0; for(i=n-len;i<n;i++) { y[yid++]=i; } for(i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len; cmp(n,sz); swap(x,y); x[sa[0]]=yid=0; for(i=1;i<n;i++) { if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len]) x[sa[i]]=yid; else x[sa[i]]=++yid; } sz=yid+1; if(sz>=n) break; } for(i=0;i<n;i++) Rank[i]=x[i]; } void getHeight(char *s,int n) { int k=0; for(int i=0;i<n;i++) { if(Rank[i]==0) continue; k=max(0,k-1); int j=sa[Rank[i]-1]; while(s[i+k]==s[j+k]) k++; height[Rank[i]]=k; } } int minv[53010][20],lg[53030]; void init_lg() { int i; lg[1]=0; for(i=2;i<52020;i++) { lg[i]=lg[i>>1]+1; } } void init_RMQ(int n) { int i,j,k; for(i=1;i<=n;i++) { minv[i][0]=height[i]; } for(j=1;j<=lg[n];j++) { for(k=0;k+(1<<j)-1<=n;k++) { minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]); } } } int lcp(int l,int r) { l=Rank[l]; r=Rank[r]; if(l>r) swap(l,r); l++; int k=lg[r-l+1]; return min(minv[l][k],minv[r-(1<<k)+1][k]); } int main(){int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);int i,j,k;for(i=0;i<n;i++){scanf("%s",str+i);}build_sa(str,n+1,128);getHeight(str,n);init_lg();init_RMQ(n);int maxn=0;for(i=1;i<n;i++){for(j=0;j+i<n;j+=i){int k=lcp(j,j+i);int now=k/i;int tj=j-(i-k%i);if(tj>=0){if(lcp(tj,tj+i)>=i-k%i)now++;}if(now>maxn)maxn=now;}}printf("%d\n",maxn+1);}}
- SPOJ题目687 Repeats(后缀数组+RMQ求重复次数最多的子串的重复次数)
- SPOJ 687 Repeats (后缀数组+RMQ 重复次数最多的连续重复子串)
- SPOJ REPEATS - Repeats(后缀数组[重复次数最多的连续重复子串])
- Repeats SPOJ (后缀数组,寻找一个重复次数最多的子串)
- poj 3693/hdu 2459 Maximum repetition substring spoj 687. Repeats ( 后缀数组 重复次数最多的连续重复子串)
- poj 3693 重复次数最多的连续重复子串(后缀数组+RMQ)
- UVA 题目11512 - GATTACA(后缀数组求出现次数最多的子串及重复次数)
- POJ 题目 3693 Maximum repetition substring(后缀数组+RMQ+枚举求最小字典序的重复次数最多的子串)
- 后缀数组(重复次数最多的连续重复子串)
- poj 1743Maximum repetition substring(后缀数组+RMQ+重复次数最多的连续重复子串))
- HDU 2459 Maximum repetition substring(后缀数组+RMQ-重复次数最多的子串)
- 【后缀数组求重复次数最多的连续重复子串】SPOJ687 POJ3693
- Maximum repetition substring+POj+后缀数组之求重复次数最多的连续重复子串
- POJ - 3693 Maximum repetition substring(后缀数组求重复次数最多的连续重复子串)
- hdu 2459 后缀数组 求最多重复次数子串
- 后缀数组(重复次数最多的连续重复子串)好poj3693+spoj687
- 后缀数组(重复次数最多的最长重复子串)—— POJ 3693
- spoj687 重复次数最多的连续重复子串(后缀数组)
- Extjs4---Checkbox,多选,全选
- 转EditText inputType 常用属性
- UI_UITextFile(单行文本输入框)
- MSXML中的SelectNodes和SelectSingleNode
- iOS 并发编程之 Operation Queues(别人博客写的很详细)
- SPOJ题目687 Repeats(后缀数组+RMQ求重复次数最多的子串的重复次数)
- ext4学习参考博客
- keil注册机激活的方法
- 搬家 to http://www.cnblogs.com/mengyou0304/
- ex3
- 实现 checkbox 全选,全不选
- maven学习4
- UE4 Quick Settings
- 保利威视教程:什么是HLS?