题目链接:https://vjudge.net/problem/POJ-3693
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11250 | Accepted: 3465 |
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
Source
题意:
给出一个字符串,求该字符串的重复次数最多的连续重复子串,输出该子串,如果有多个答案,输出字典序最小的那个。
题解:
SPOJ - REPEATS的加强版,需要输出目标串。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; bool cmp(int *r, int a, int b, int l)
{
return r[a]==r[b] && r[a+l]==r[b+l];
} int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
int t1[MAXN], t2[MAXN], c[MAXN];
void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
{
n++;
int i, j, p, *x = t1, *y = t2;
for(i = ; i<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[i] = str[i]]++;
for(i = ; i<m; i++) c[i] += c[i-];
for(i = n-; i>=; i--) sa[--c[x[i]]] = i;
for(j = ; j<=n; j <<= )
{
p = ;
for(i = n-j; i<n; i++) y[p++] = i;
for(i = ; i<n; i++) if(sa[i]>=j) y[p++] = sa[i]-j; for(i = ; i<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[y[i]]]++;
for(i = ; i<m; i++) c[i] += c[i-];
for(i = n-; i>=; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y);
p = ; x[sa[]] = ;
for(i = ; i<n; i++)
x[sa[i]] = cmp(y, sa[i-], sa[i], j)?p-:p++; if(p>=n) break;
m = p;
} int k = ;
n--;
for(i = ; i<=n; i++) Rank[sa[i]] = i;
for(i = ; i<n; i++)
{
if(k) k--;
j = sa[Rank[i]-];
while(str[i+k]==str[j+k]) k++;
height[Rank[i]] = k;
}
} int dp[MAXN][], mm[MAXN];
void initRMQ(int n, int b[])
{
mm[] = -;
for(int i = ; i<=n; i++)
dp[i][] = b[i], mm[i] = ((i&(i-))==)?mm[i-]+:mm[i-];
for(int j = ; j<=mm[n]; j++)
for(int i = ; i+(<<j)-<=n; i++)
dp[i][j] = min(dp[i][j-], dp[i+(<<(j-))][j-]);
} int RMQ(int x, int y)
{
if(x>y) swap(x, y);
x++;
int k = mm[y-x+];
return min(dp[x][k], dp[y-(<<k)+][k]);
} char str[MAXN];
int Len[MAXN];
int main()
{
int kase = ;
while(scanf("%s", str) && str[]!='#')
{
int n = strlen(str);
for(int i = ; i<n; i++)
r[i] = str[i];
r[n] = ;
DA(r, sa, Rank, height, n, );
initRMQ(n, height); int times = , cnt = ;
for(int len = ; len<=n; len++)
for(int pos = ; pos+len<n; pos += len)
{
int LCP = RMQ(Rank[pos], Rank[pos+len]);
int supplement = len - LCP%len;
int k = pos - supplement;
if(k>= && LCP%len && RMQ(Rank[k],Rank[k+len])>=supplement)
LCP += supplement;
/*
当不能加上supplement时,以pos为起点的子串不一定是字典序最小,
而应该在[pos, pos+LCP%len]里面取最小,所以为了取得字典序最小,
先不记录位置,而只记录出现次数最大的情况下有多少种循环节,等
统计完之后,再按排名从前到后,为每个sa[i]匹配循环节,匹配成功
即为答案。
*/
int tmp = LCP/len+;
if(tmp>times) times = tmp, Len[cnt=] = len;
else if(tmp==times) Len[++cnt] = len;
}
int L, R, flag = true;
for(int i = ; i<=n && flag; i++)
for(int j = ; j<=cnt; i++)
{
int len = Len[j];
int LCP = RMQ(i, Rank[sa[i]+len]);
if(LCP>=len*(times-))
{
L = sa[i];
R = sa[i]+len*times-;
flag = false;
break;
}
}
printf("Case %d: ", ++kase);
for(int i = L; i<=R; i++)
putchar(str[i]);
putchar('\n');
}
}