POJ 3693 - Maximum repetition substring (后缀数组)

49 篇文章 0 订阅
3 篇文章 0 订阅

Maximum repetition substring
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7099 Accepted: 2118

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

[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 WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef 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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值