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

0 0
原创粉丝点击