SPOJ687---repeats-repeats (suffix array +rmq)

Source: Internet
Author: User

A string S is called an (k,l)-repeat if S is obtained by concatenating k>=1 times some seed string T with length l>= 1. For example, the string

s = Abaabaabaaba

is a (4,3)-repeat with t = ABA as its seed string. That's, the seed string T is 3 characters long, and the whole string s are obtained by repeating T 4 times.

Write a program for the following Task:your program is given a long string u consisting of characters ' a ' and/or ' B ' as I Nput. Your program must find some (k,l)-repeat, occurs as substring within U with K as large as possible. For example, the input string

U = Babbabaabaabaabab

Contains the underlined (4,3)-repeat s starting at position 5. Since u contains no other contiguous substring with more than 4 repeats, your program must output the maximum K.

Input

The first line of the input contains h-the number of test cases (H <= 20). H test Cases follow. First line of all test cases is n-length of the input string (n <= 50000), the next n lines contain the input string , one character (either ' a ' or ' B ') per line, in order.
Output

For each test cases, you should write exactly one Interger K in a line-the repeat count of is maximized.
Example

Input:
1
17
B
A
B
B
A
B
A
A
B
A
A
B
A
A
B
A
B

Output:
4

Since a (4, 3)-repeat is found starting at the 5th character of the input string.

Method or the method of the paper, I use 2 suffix array, the other one is the string inversion to find out, and then respectively LCP

/************************************************************************* > File name:spoj-687.cpp > Aut Hor:alex > Mail: [email protected] > Created time:2015 April 07 Tuesday 18:16 49 seconds ******************************* *****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;classsuffixarray{ Public:Static Const intN =52000;intInit[n];intX[n];intY[n];intRank[n];intSa[n];intHeight[n];intBuc[n];intLog[n];intdp[n][ -];intSizevoidClear () {size =0; }voidInsertintN) {init[size++] = n; }BOOLcmpint*r,intAintBintL) {return(R[a] = = R[b] && r[a + l] = = R[b + L]); }voidGetsa (intm = the)//m is generally the maximum value of +1{Init[size] =0;intL, p, *x = x, *y = y, n = size +1; for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n;            ++i) {++buc[x[i] = init[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0;            -i) {sa[--buc[x[i]] = i; } for(L =1, p =1; L <= N && p < n; m = p, l *=2) {p =0; for(inti = n-l; I < n;                ++i) {y[p++] = i; } for(inti =0; I < n; ++i) {if(Sa[i] >= L)                    {y[p++] = sa[i]-l; }                } for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n;                ++i) {++buc[x[y[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0;                -i) {sa[--buc[x[y[i]]] = y[i]; }intI for(Swap (x, y), x[sa[0]] =0, p =1, i =1; I < n; ++i) {X[sa[i]] = cmp (y, Sa[i-1], Sa[i], L)? P-1: p++; }            }        }voidGetHeight () {inth =0, n = size; for(inti =0; I <= N;            ++i) {Rank[sa[i]] = i; } height[0] =0; for(inti =0; I < n; ++i) {if(H >0) {--h; }intJ =sa[rank[i]-1]; for(; i + H < n && j + H < n && init[i + h] = = Init[j + h]; ++h); Height[rank[i]-1] = h; }        }//preprocessing logarithm of each number for RMQ, constant optimization        voidInitlog () {log[0] = -1; for(inti =1; i < N; ++i) {Log[i] = (I & (i-1)) ? Log[i-1]: Log[i-1] +1; }        }voidINITRMQ () {initlog ();intn = size;intLimit for(inti =0; I < n; ++i) {dp[i][0] = Height[i]; } for(intj =1; J <= Log[n]; ++J) {limit = N-(1<< j); for(inti =0; I <= limit; ++i) {Dp[i][j] = min (Dp[i][j-1], Dp[i + (1<< (J-1))][j-1]); }            }        }intLCP (intAintb) {intT            A = Rank[a]; b = Rank[b];if(A > B)            {Swap (A, b);            }--b; t = log[b-a +1];returnMin (dp[a][t], dp[b-(1<< T) +1][t]); }}SA1, SA2;Charbuf[52000];Charstr[Ten];intMain () {intTscanf("%d", &t); while(t--)        {sa1.clear (); Sa2.clear ();intNscanf("%d", &n); for(inti =0; I < n; ++i) {scanf('%s ', str); Sa1.insert ((int) str[0]); Buf[i] = str[0]; } Buf[n] =' + '; for(inti = n-1; I >=0; -I.) {Sa2.insert (int) buf[i]); } Sa1.getsa ((int)(' B ') +1); Sa2.getsa ((int)(' B ') +1);        Sa1.getheight ();        Sa2.getheight ();        SA1.INITRMQ (); SA2.INITRMQ ();intAns =1, S, ans_l =0; for(intL =1; L <= N; ++L) { for(inti =0; I < n; ++i) {intA = L * i;intb = L * (i +1);if(b >= N) { Break; }intL1 = SA1. LCP (A, b);intL2 = SA2. LCP (N-a-1, N-b-1);intK = L1 + L2;if(k >0) {--k; }intCNT = k/l +1;if(cnt > ANS)                    {ans = cnt;                    s = A;                ans_l = L; }             }        }printf("%d\n", ans); }return 0;}

SPOJ687---repeats-repeats (suffix array +rmq)

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.