POJ 3693 Maximum repetition substring (suffix array to find the longest repeating substring)

Source: Internet
Author: User

The main problem: similar to spoj687, is that when the same length is required to output a minimum sequence of the dictionary sequence.

Disintegration idea: This time need to enumerate all from I to d = i-l/i (d = i-l%i) position, and then record the maximum value of the same time, to find out the dictionary order of the smallest.

Maximum Repetition substring
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7418 Accepted: 2217

Description

The repetition number of a string is defined as the maximum number R Such so 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 is 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 is not being greater than 100,000.

The last test was 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 R Epetition number. If There is multiple substrings of maximum repetition number, print the lexicographically smallest one.

Sample Input

ccabababcdaabbccaa#

Sample Output

Case 1:abababcase 2:aa
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include < iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include < stack> #include <ctime> #include <map> #include <set> #define EPS 1e-9///#define M 1000100///#define ll __int64#define ll Long long///#define INF 0x7ffffff#define inf 0x3f3f3f3f#define PI 3.1415926535898#define Zero (x) ((FA BS (x) <eps)? 0:x) #define MOD 1000000007#define Read () freopen ("Autocomplete.in", "R", stdin) #define Write () freopen ("    Autocomplete.out "," w ", stdout) #define CIN () Ios::sync_with_stdio (false) using namespace Std;inline int Read () {char ch;    BOOL flag = FALSE;    int a = 0; while (!) ( ((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) | |    (ch = = '-')));        if (ch! = '-') {a *= 10;    A + = ch-' 0 ';    } else {flag = true; } while (((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) {        A *= 10;    A + = ch-' 0 ';    } if (flag) {a = A; } return A;        void write (int a) {if (a < 0) {Putchar ('-');    A =-A;    } if (a >=) {write (A/10); } putchar (a% 10 + ' 0 ');}    const int MAXN = 100050;int WA[MAXN], WB[MAXN], WV[MAXN], ws1[maxn];int sa[maxn];int cmp (int *r, int A, int b, int l) { return R[a] = = R[b] && r[a+l] = = R[b+l];}    void da (int *r, int *sa, int n, int m) {int I, J, p, *x = WA, *y = WB;    for (i = 0; i < m; i++) ws1[i] = 0;    for (i = 0; i < n; i++) ws1[x[i] = r[i]]++;    for (i = 1; i < m; i++) ws1[i] + = ws1[i-1];    for (i = n-1; I >= 0; i--) sa[--ws1[x[i]] = i;        for (j = 1, p = 1; p < n; j <<= 1, m = p) {for (P = 0, i = n-j; i < n; i++) y[p++] = i;        for (i = 0; i < n; i++) if (Sa[i] >= j) y[p++] = sa[i]-j;        for (i = 0; i < n; i++) wv[i] = X[y[i];        for (i = 0; i < m; i++) ws1[i] = 0; for (i = 0; i <N        i++) ws1[wv[i]]++;        for (i = 1; i < m; i++) ws1[i] + = ws1[i-1];        for (i = n-1; I >= 0; i--) sa[--ws1[wv[i]] = y[i];    for (Swap (x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp (y, sa[i-1], Sa[i], j)? p-1:p++;    }}int RANK[MAXN], height[maxn];void calheight (int *r, int *sa, int n) {int I, j, k = 0;    for (i = 1; I <= n; i++) rank[sa[i]] = i;    for (int i = 0; i < n; height[rank[i++]] = k) for (k?k--:0, j = sa[rank[i]-1]; r[i+k] = = R[j+k]; k++); return;}    int dp[maxn][30];void RMQ (int len) {for (int i = 1; I <= len; i++) dp[i][0] = Height[i];  for (int j = 1; 1<<j <= Maxn; j + +) {for (int i = 1; i+ (1<<j)-1 <= len; i++) Dp[i][j]    = Min (dp[i][j-1], dp[i+ (1<< (j-1))][j-1]);    }}int lg[maxn];int querry (int l, int r) {int k = lg[r-l+1]; return min (Dp[l][k], dp[r-(1<<k) +1][k]);}    void Init () {lg[0] =-1; for (int i = 1; i < MAXN; ++i) lg[i] = lg[I&GT;&GT;1] + 1;}    int Seq[maxn];char str[maxn];void Del (int n, int case) {int Max = 0;    String TMX, tmp;            for (int i = 1, i <= n/2; i++) {for (int j = 0; i*j+i < n; j + +) {int l = rank[i*j];            int r = Rank[i*j+i];            if (L > R) Swap (L, R);            int x = Querry (l+1, R);            int y = 0;                if (Max <= x/i + 1) {///assign is an assignment function of string string.assign (const char *, int, int);                Tmp.assign (str, i*j, i* (x/i+1));                    if (X/i+1 > Max) {max = x/i+1;                TMX = tmp;                    } else {int xn = tmx.size ();                if (!xn | | TMX > TMP) TMX = tmp;            }} int xp = i-x%i; if (XP && j) {for (int k = XP; k < i; k++) {L = Ran k[I*J-K];                    r = Rank[i*j+i-k];                    if (L > R) Swap (L, R);                    y = Querry (l+1, R);                    int YP = y/i+1;                    if (YP < Max) continue;                    Tmp.assign (str, i*j-k, I*YP);                        if (Max < YP) {TMX = tmp;                    Max = YP;                        } else {int yn = tmx.size ();                    if (!yn | | TMX > TMP) TMX = tmp;    }}}}} printf ("Case%d:", case); Cout<<tmx<<endl;}    int main () {init ();    int case = 1;        while (scanf ("%s", str) && str[0]! = ' # ') {int n = strlen (str);        for (int i = 0; i < n; i++) seq[i] = str[i]-' a ' +1;        Seq[n] = 0;        Da (seq, SA, n+1, 27);        Calheight (seq, SA, N);        RMQ (n); Del (N, case++); } return 0;}


POJ 3693 Maximum repetition substring (suffix array to find the longest repeating substring)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.