WHU---1084-continuous technique (suffix array + two points)

Source: Internet
Author: User

Description
No matter what martial arts, how many will have one or two consecutive techniques appear, these continuous skills are often invented the martial arts people's customary action, if these actions were analyzed by the opponent, it is easy to be the opponent to grasp the opportunity. For example, in the song Wind Sword spectrum there is a type called upwind pride is the following action:
Bayonetted, sharpen, kick, stab, sharpen,
It was obvious that the stab-cut had appeared 4 times, while the stab-cut-stab-cut was two times in a row.

Now Liu Baiyu got a demon-taught palm law, want you to help to analyze the longest and appear as many of the continuous technology, of course, he was embarrassed to bother you for too long, just want you to tell it how long the continuous technology and how many times can be.

Note: Only one action, or one continuous action, cannot be considered a continuous technique.
Input
Input contains multiple sets of data, each set of data contains two rows, the first line is a number n (1<=n<=1000), the second row has n numbers, each number represents a basic action in the Palm method, such as splitting, shaking, shooting, point, and so on.
Output
For each palm of the input, the length of the longest continuous technique and the number of occurrences are output. If there is no such continuous technology, then output-1.
Sample Input
1
2
10
4 1 2 1 2 5 1 2 1 2
8
1 2 1 2 1 2 1 2
Sample Output
-1
5 S
5 S
Hint
Source
Gardon-dygg ' s Contest 4

Suffix array + Two can find the longest repeat substring, and then for each group of suffixes, select the first or last suffix (in terms of the position of the relationship) must be the best, so go through the time to know the maximum number of repetitions

/************************************************************************* > File Name:WHU1084.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 April 10 Friday 16:39 10 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;intarr[ -];intxis[ -];intCntintSearchintVal) {intL =1, R = CNT, mid; while(L <= R) {mid = (L + r) >>1;if(Xis[mid] = = val) { Break; }Else if(Xis[mid] > val) {R = mid-1; }Else{L = mid +1; }    }returnMid;}classsuffixarray{ Public:Static Const intN = -;intInit[n];intX[n];intY[n];intRank[n];intSa[n];intHeight[n];intBuc[n];intLog[n];intdp[n][ -];intSize vector <int>TaskvoidClear () {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; }        }BOOLCheckintK) {intL = sa[1], R = sa[1]; for(inti =1; i < size; ++i) {if(Height[i] >= k) {L = min (l, Sa[i +1]); r = Max (R, Sa[i +1]); }Else{if(R-l >= k) {return 1; } L = r = sa[i +1]; }            }return 0; }voidSolve () {intL =1, r = size/2, Mid;intAns =-1; while(L <= R) {mid = (L + r) >>1;if(Check (mid)) {L = mid +1;                Ans = mid; }Else{R = mid-1; }            }if(ans = =-1)            {printf(" -1\n");return;            } task.clear (); Task.push_back (sa[1]);intRET =0; for(inti =1; i < size; ++i) {if(Height[i] >= ans) {Task.push_back (sa[i +1]); }Else{Sort (Task.begin (), Task.end ());intTMP1 =1, TMP2 =1, isize = Task.size (), last = task[0]; for(intj =1; J < Isize; ++J) {if(Task[j]-last >= ans)                            {++TMP1;                        last = Task[j]; }} last = Task[isize-1]; for(intj = isize-2; J >=0; --J) {if(Last-task[j] >= ans)                            {++TMP2;                        last = Task[j];                    }} ret = max (ret, Max (TMP1, TMP2));                    Task.clear (); Task.push_back (Sa[i +1]); }            }printf("%d%d\n", ans, ret); }}sa;intMain () {intN while(~scanf("%d", &n)) {sa.clear (); CNT =0; for(inti =0; I < n; ++i) {scanf("%d", &arr[i]);        XIS[++CNT] = Arr[i]; } sort (XIs +1, XIs +1+ CNT); CNT = unique (XIs +1, XIs +1+ CNT)-XIs-1; for(inti =0; I < n; ++i) {intval = search (Arr[i]);        Sa.insert (Val); } Sa.getsa (CNT +1);        Sa.getheight ();    Sa.solve (); }return 0;}

WHU---1084-continuous technique (suffix array + two points)

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.