Poj 3693 maximum repetition substring (troublesome suffix array)

Source: Internet
Author: User
Tags repetition
Maximum repetition substring
Time limit:1000 ms   Memory limit:65536 K
Total submissions:6638   Accepted:2007

Description

The repetition number of a string is defined as the maximum numberRSuch that the string can be partitionedRSame 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 '#'.

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

2008 Asia Hefei Regional Contest online by USTC

Question:

First, the degree of repetition of a string is defined. That is, a string consists of a substring that is repeated K times. The maximum K is the repetition of the string. For example

The ababababab degree is 4. The ABC degree is 1. Now we will give you a string of no more than 10 ^ 5 and ask you to find the substring with the highest degree. If there are multiple solutions. Find the smallest Lexicographic Order.

Ideas:

It is easy to think of that the enumerated length is l, and then the string with the length of L appears several times at most consecutively.
If the string with the length of L is repeated, then St [0], St [L], St [2 * l]… St [K * l] must have two consecutive occurrences in the string. Otherwise, the length must not exceed 2 * L. Then we will enumerate the two consecutive characters, and then compare the two characters to see how far they can be matched.
That is to say, it is matched before and after St [I * l + L]. Here we query suffix (I * l), suffix (I * L + l) the longest public prefix
The rank value can be used to locate the ranking between I * l and I * L + L. What we want to query is the minimum value of the height of this range, which is preprocessed by rmq.
The query complexity is 0 (1). If the LCP length is set to m, the answer is obviously M/L + 1, however, this is only the starting point of I * l and I * L + L, but one thing is certain. If the target substring contains I * l and I * L + L. Then I * l must match I * L + L. Because p must match P + L in the target string. In this way, the length of the substring is L. What should be solved first is that the starting point is not in these two locations. To get M/L + 1, we can try to increase the answer. If M % L! = 0 we can add the length to an integer multiple of L. That is, add the character (L-M % L) in front to see if the answer can be increased. Why is this possible? Because we have to make it, and the answer will become bigger, and expansion will definitely not work. Because it does not match later. But why is there so much extension (L-M % L. It cannot be smaller than this one. Because it still does not reach the integer multiple of L. Better than this. This value will certainly work. Because p matches P + L. Since more than this is achieved. It is too big to translate to the Right to match M % L. Then why do we only extend the length of 1. Do not expand multiple. Because you enumerate each I * l and I * L + L. You can expand two or more of the preceding I * l and I * L + L. After this step is completed, we can only obtain the value of the maximum length of a degree. The remaining job is to find the smallest Lexicographic Order. The SA array is used for enumeration. The first group obtained must be the smallest Lexicographic Order.

For details, see the code:

# Include <algorithm> # include <iostream> # include <string. h> # include <stdio. h> using namespace STD; const int INF = 0x3f3f3f3f; const int maxn = 100010; typedef long ll; int SA [maxn], T1 [maxn], T2 [maxn], he [maxn], rk [maxn], CT [maxn]; int rmq [25] [maxn], lg [maxn], Alen [maxn], PTR; int N, m, ans; char TXT [maxn], ATX [maxn]; void rmq_init () {int I, j; for (I = 0; I <n; I ++) rmq [0] [I] = He [I]; for (I = 1; I <= lg [N]; I ++) for (j = 0; J + (1 <I)-1 <n; j + +) Rmq [I] [J] = min (rmq [I-1] [J], rmq [I-1] [J + (1 <(I-1)]);} int rmq_min (int l, int R) {int TMP = lg [R-l + 1]; return min (rmq [TMP] [L], rmq [TMP] [R-(1 <TMP) + 1]);} void prermq () {int I; lg [0] =-1; for (I = 1; I <maxn; I ++) lg [I] = lg [I> 1] + 1;} void getsa (char * st) {int I, K, P, * x = T1, * Y = t2; for (I = 0; I <m; I ++) CT [I] = 0; for (I = 0; I <n; I ++) CT [x [I] = sT [I] ++; for (I = 1; I <m; I ++) CT [I] + = CT [I-1]; for (I = n-1; I> = 0; I --) sa [-- CT [x [I] = I; for (k = 1, P = 1; P <n; k <= 1, m = P) {for (P = 0, 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 ++) CT [I] = 0; for (I = 0; I <n; I ++) CT [x [Y [I] ++; for (I = 1; I <m; I ++) CT [I] + = CT [I-1]; for (I = n-1; I> = 0; I --) sa [-- CT [x [Y [I] = Y [I]; for (SWAp (x, y), P = 1, X [SA [0] = 0, 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 ++ ;}} void gethe (char * st) {int I, j, k = 0; for (I = 0; I <n; I ++) rk [SA [I] = I; for (I = 0; I <n-1; I ++) {If (k) k --; j = sa [rk [I]-1]; while (ST [I + k] = sT [J + k]) K ++; he [rk [I] = K ;}} int main () {int I, j, L, P, TP, low, hi, ID, PL, TT,, b, CAS = 1; prermq (); While (scanf ("% s", txt), TXT [0]! = '#') {N = strlen (txt); N ++; M = 150; getsa (txt); gethe (txt); rmq_init (); ans = 1, id = PTR = 0, PL = 1; for (L = 1; L <n; l ++) {for (P = L; P <n; P + = L) {LOW = min (rk [P-l], rk [p]); Hi = max (rk [P-l], rk [p]); tt = rmq_min (low + 1, hi); TP = TT/L + 1; if (TP> ans) ans = TP, PTR = 0, alen [PTR ++] = L; else if (TP = ans & Alen [ptr-1]! = L) Alen [PTR ++] = L; If (TT % L) {A = P-l-(L-TT % L ); B = P-(L-TT % L); if (a> = 0) {LOW = min (rk [a], rk [B]); hi = max (rk [a], rk [B]); TT = rmq_min (low + 1, hi); TP = TT/L + 1; if (TP> ans) ans = TP, PTR = 0, Alen [PTR ++] = L; else if (TP = ans & Alen [ptr-1]! = L) Alen [PTR ++] = L ;}}} for (I = 1, PL =-1; I <n; I ++) // enumerate SA {for (j = 0; j <PTR; j ++) {TP = sa [I] + Alen [J]; If (TP> = N) continue; TP = rk [TP]; A = min (I, TP); B = max (I, TP); If (rmq_min (a + 1, B)> = (ans-1) * Alen [J]) {PL = Alen [J]; id = min (SA [a], sa [B]); break ;}} if (pl! =-1) break;} PL = ans * pl; for (I = 0; I <pl; I ++) ATX [I] = TXT [ID + I]; ATX [pl] = 0; printf ("case % d: % s \ n", CAS ++, ATX) ;}return 0 ;}/ * dabcabcebaccdbaccdbacbdbacbd */


Poj 3693 maximum repetition substring (troublesome suffix array)

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.