POJ 3693 Maximum Repetition Substring 后缀数组
来源:互联网 发布:电魂网络 编辑:程序博客网 时间:2024/05/01 03:03
没赶上23:59分发帖?
bzoj挂了后顿时感到人生无光。
代码总觉得有问题到时再改。。
#include <cstdio>#include <algorithm>#include <cstring>using namespace std;#define N 100010#define rep(i,j,k) for(i=j;i<k;i++)#define FOR(i,j,k) for(i=j;i<=k;i++)char str[N];int s[N];template<typename T>struct SuffixArray { T *r; int sa[N], wa[N], wb[N], z[N], bucket[N], rank[N], height[N], n; int dp[N][33]; void init(T *s, int n, int m) { (r = s)[this->n = n] = 0; da(m); calHeight(); initRMQ(); } void sort(int *sa, int *x, int *y, int n, int m) { int i; rep(i,0,m) bucket[i] = 0; rep(i,0,n) bucket[x[y[i]]]++; rep(i,1,m) bucket[i] += bucket[i-1]; for(i=n-1;i>=0;i--) sa[--bucket[x[y[i]]]] = y[i]; } void da(int m) { int n=this->n+1; int i,j,p,*x=wa,*y=wb,*t; rep(i,0,n) x[i]=r[i],z[i]=i; sort(sa, x, z, n, m); for(j=1,p=1;p<n;j*=2,m=p) { p = 0; rep(i,n-j,n) y[p ++] = i; rep(i,0,n) if(sa[i] >= j) y[p ++] = sa[i] - j; sort(sa, x, y, n, m); t=x,x=y,y=t;p=1;x[sa[0]]=0; rep(i,1,n) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++; } } void calHeight() { int i, j, k = 0; FOR(i,1,n) rank[sa[i]] = i; rep(i,0,n) { if (k) k--; j = sa[rank[i]-1]; while(r[i+k]==r[j+k]) k++; height[rank[i]] = k; } } void initRMQ() { int i, j; FOR(i,1,n) dp[i][0]=height[i]; for(j=1;(1<<j)<=n;j++) for(i=1;i+(1<<j)-1<=n;i++) dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } int query(int ll, int rr) { int k = 0; ll = rank[ll]; rr = rank[rr]; if(ll > rr) swap(ll,rr); ll++; while((1<<(k+1))<=rr-ll+1) k++; return min(dp[ll][k],dp[rr-(1<<k)+1][k]); }};SuffixArray<int> sa;int main() { int text = 0, i, j, p, r, cnt, tol, k, m, ans, pos, ansl, len; while(scanf("%s", str) && str[0] != '#') { len = strlen(str); ans = pos = 0; rep(i,0,len) s[i] = str[i] - 'a' + 1; sa.init(s, len); FOR(i,1,len/2) for (j = 0; j < len - i; j += i) { if (str[j] != str[j + i]) continue; k = sa.query(j, j + i); tol = k / i + 1; r = i - k % i; p = j; cnt = 0; for (m = j - 1; m > j - i && str[m] == str[m + i] && m >= 0; --m) { ++cnt; if (cnt == r) ++tol; if (cnt == r || sa.rank[p] > sa.rank[m]) p = m; } if (ans < tol || ans == tol && sa.rank[pos] > sa.rank[p]) ans = tol, pos = p, ansl = tol * i; } printf("Case %d: ", ++text); if(ans < 2) { char ch = 'z'; rep(i,0,len) if(str[i] < ch) ch = str[i]; printf("%c\n", ch); continue; } rep(i,pos,pos + ansl) printf("%c", str[i]); printf("\n"); } return 0;}
Maximum repetition substring
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8600 Accepted: 2612
Description
The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same consecutive substrings. For example, the repetition number of “ababab” is 3 and “ababa” is 1.
Given a string containing lowercase letters, you are to find a substring of it with maximum repetition number.
Input
The input consists of multiple test cases. Each test case contains exactly one line, which
gives a non-empty string consisting of lowercase letters. The length of the string will not be greater than 100,000.
The last test case is followed by a line containing a ‘#’.
Output
For each test case, print a line containing the test case number( beginning with 1) followed by the substring of maximum repetition number. If there are multiple substrings of maximum repetition number, print the lexicographically smallest one.
Sample Input
ccabababc
daabbccaa
#
Sample Output
Case 1: ababab
Case 2: aa
- poj 3693 Maximum repetition substring //后缀数组
- poj 3693 Maximum repetition substring (后缀数组)
- poj 3693 Maximum repetition substring(后缀数组)
- poj 3693 Maximum repetition substring(后缀数组)
- 【后缀数组】【poj 3693】Maximum repetition substring
- POJ 3693 Maximum Repetition Substring 后缀数组
- poj 3693 Maximum repetition substring(后缀数组好题)
- POJ 3693 Maximum repetition substring(RMQ+后缀数组)
- POJ 3693 Maximum repetition substring (后缀数组)
- 【后缀数组】 HDOJ 2459 && POJ 3693 Maximum repetition substring
- poj 3693 Maximum repetition substring(有点麻烦的后缀数组)
- POJ 3693 Maximum repetition substring(后缀数组神题)
- [后缀数组+枚举] hdu 2459 && poj 3693 Maximum repetition substring
- poj 3693 Maximum repetition substring 后缀数组+RMQ
- POJ 3693 - Maximum repetition substring (后缀数组)
- POJ 3693 Maximum repetition substring 后缀数组 暴力 rmq
- POJ 3693 Maximum repetition substring(后缀数组)
- POJ 3693 Maximum repetition substring 后缀数组 + RMQ预处理
- 常量字符串出现的segmentation fault(core dumped)
- java并发编程——线程异常处理\资源共享的问题\ThreadLocal
- 隔代沟通的阻碍
- BM算法
- iOS开发中自定义相册功能性能改善
- POJ 3693 Maximum Repetition Substring 后缀数组
- 安卓开发:活动的四种启动模式
- SSH连接服务器出错解决方法,
- [Python开发] sphinx学习
- CentOS安装apache,MysqL,PHP
- leetcode笔记:Game of Life
- 开发jpa应用时异常
- C#文件操作的两种方式
- Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate (数学)