POJ 3693 - Maximum repetition substring (后缀数组)
来源:互联网 发布:网络域名怎么注册 编辑:程序博客网 时间:2024/05/01 00:42
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
ccabababcdaabbccaa#
Sample Output
Case 1: abababCase 2: aa
Source
[Submit] [Go Back] [Status] [Discuss]
题意:
给一个字符串,求一个重复次数最多的重复子串。
后缀数组经典应用,09年罗穗骞论文上的题目
他说了往后的匹配,但是没有说往前匹配的方法
#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define P64I1(a) printf(oform1, (a))#define REP(i, n) for(int (i)=0; (i)<n; (i)++)#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 1e-9;const double PI = (4.0*atan(1.0));const int maxn = 200000 + 20;char str[maxn];int s[maxn], sa[maxn], t[maxn], t2[maxn], c[maxn];void build_sa(int * s, int * sa, int n, int m) { int i, *x = t, *y = t2; for(i=0; i<m; i++) c[i] = 0; for(i=0; i<n; i++) c[x[i] = s[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(int k = 1; k <= n; k<<=1) { int p = 0; for(i=n-k; i<n; i++) y[p++] = i; for(i=0; i<n; i++) if(sa[i] >= k) y[p++] = sa[i]-k; for(i=0; i<m; i++) c[i] = 0; for(i=0; i<n; i++) c[x[y[i]]]++; for(i=0; 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]] = y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k] ? p-1 : p++; if(p >= n) break; m = p; }}int rank[maxn], height[maxn];void getHeight(int * s, int * sa, int n) { int i, j, k = 0; for(i=0; i<n; i++) rank[sa[i]] = i; for(i=0; i<n; i++) { if(k) k--; int j = sa[rank[i]-1]; while(s[i+k] == s[j+k]) k++; height[rank[i]] = k; }}int RMQ_d[maxn][20];void RMQ_init(int * A, int n) { for(int i=1; i<=n; i++) RMQ_d[i][0] = A[i]; for(int j=1; (1<<j) <= n; j++) for(int i=1; i+j-1 <= n; i++) RMQ_d[i][j] = min(RMQ_d[i][j-1], RMQ_d[i+(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 = 0; while((1<<(k+1)) <= R-L+1) k++; return min(RMQ_d[L][k], RMQ_d[R-(1<<k)+1][k]);}int A[maxn];int main() { int kase = 1; while(scanf("%s", str) != EOF && str[0] != '#') { int n = strlen(str); for(int i=0; i<n; i++) s[i] = str[i] - 'a' + 1; s[n] = 0; build_sa(s, sa, n+1, 27); getHeight(s, sa, n+1); /* for(int i=0; i<=n; i++) { printf("%d (%02d) : ", height[i], sa[i]); for(int j=sa[i]; j<n; j++) putchar('a'+s[j]-1); putchar('\n'); } */ // test n RMQ_init(height, n+1); int pos = 0, ans = 0, ansn = 0; int cnt = 0; for(int len=1; len<=n/2; len++) { for(int i=0; i+len<n; i+=len) { int tot = LCP(i, i+len); int m = len - (tot%len); m = i - m; if(m >= 0 && m+len < n && tot%len) tot = max(tot, LCP(m, m+len)); tot = tot / len + 1; if(tot > ans) cnt = 0; if(tot >= ans) { ans = tot; A[cnt++] = len; } } } int len = 0; int flag = true; for(int i=1; i<=n && flag; i++) { for(int j=0; j<cnt; j++) { int tl = A[j]; if(LCP(sa[i], sa[i]+tl) >= (ans-1)*tl) { flag = false; pos = sa[i]; len = tl * ans; break; } } } str[pos+len] = '\0'; printf("Case %d: %s\n", kase++, str+pos); } return 0;}
- 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 (后缀数组)
- POJ 3693 - Maximum repetition substring (后缀数组)
- POJ 3693 Maximum repetition substring(后缀数组)
- POJ 3693 Maximum repetition substring(后缀数组+ST)
- poj 3693 Maximum repetition substring (后缀数组+RMQ)
- 【后缀数组】 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
- c 选择排序
- 【无线互联】EGORefreshTableHeaderView下拉刷新 原理
- Javascript 浮点运算问题
- nyoj187快速查找素数
- 14.10.4输入无限组数据到数组的方法(数组无边界)
- POJ 3693 - Maximum repetition substring (后缀数组)
- 设计模式之命令模式
- 卡特兰数
- opencv2.4.5正确调用SurfFeatureDetector,SiftFeatureDetector
- Ubuntu14.04引导菜单修复
- Eclipse4.4安装旧版本插件报错 Failed to prepare partial IU
- 设计模式之状态模式
- POJ2739
- 今天是我的生日