Hdu 5442 favorite Donut suffix array

Source: Internet
Author: User

Favorite Donut

Time Limit:1 Sec

Memory limit:256 MB

Topic Connection http://acm.hdu.edu.cn/showproblem.php?pid=5442
Description

Lulu has a sweet tooth. Her favorite food is ring donut. Everyday she buys a ring donut from the same bakery. A Ring Donut is consists of n parts. Every part have its own sugariness the can is expressed by a letter from A to Z (from low to High), and a ring donut can B E expressed by a string whose i-th character represents the sugariness of the i−th part in clockwise order. Note that Z is the sweetest, and both parts is equally sweet if they has the same sugariness.

Once Lulu eats a part of the donut, she must continue to eat it uneaten adjacent part until all parts is eaten. Therefore, she had to eat either clockwise or counter-clockwise after her first bite, and there is 2n ways to eat the Rin G Donut of N parts. For example, Lulu have 6 ways to eat a ring donut ABC:ABC,BCA,CAB,ACB,BAC,CBA. Lulu likes eating the sweetest part first, so she actually prefer the the-the-the-greatest-lexicographic order. If there is, or more lexicographic maxima, then she'll prefer the-the-part whose starting part have the minimum index in C Lockwise order. If the ways start at the same part, then she'll prefer eating the donut in clockwise order. Please compute the Eat the donut she likes most.

Input

First line contain one integer t,t≤20, which means the number in test case.

For each test case, the first line contains one integer n,n≤20000, which represents how many parts the Ring donut have. The next line contains a string consisted of n lowercase alphabets representing the ring donut.

Output
You should print one line for each test case, consisted of the integers, which represents the starting point (from 1 to N) and the direction (0 for clockwise and 1 for counterclockwise).
Sample Input

2
4
Abab
4
Aaab

Sample Output

2 0
4 0

HINT

Test instructions

Give you a ring, the character on the ring indicates the degree of sweetness, you can eat it, you can eat it back.

Then ask how you eat can make the most sweetness

Exercises

Suffix array, run the pros and cons, and get the two largest strings of length n

And then O (n) a little bit better.

Code:

//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 50000#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f; inline ll read () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************ints[2][MAXN];intN,nn;intSA[MAXN], RK[MAXN], HEIGHT[MAXN];intWA[MAXN], WB[MAXN], WV[MAXN], WD[MAXN];intcmpint*r,intAintBintl) {    returnR[a] = = R[b] && r[a+l] = = r[b+l];}voidBuild_sa (int*r,intNintm) {    intI, J, p, *x = WA, *y = WB, *T;  for(i =0; I < m; i + +) wd[i] =0;  for(i =0; I < n; i + +) Wd[x[i]=r[i]] + +;  for(i =1; I < m; i + +) Wd[i] + = wd[i-1];  for(i = n1; I >=0; I--) sa[--wd[x[i]] =i;  for(j =1, p =1; P < n; J *=2, M =p) {         for*pb=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 + +) wd[i] =0;  for(i =0; I < n; i + +) Wd[wv[i]] + +;  for(i =1; I < m; i + +) Wd[i] + = wd[i-1];  for(i = n1; I >=0; I--) sa[--wd[wv[i]] =Y[i];  for(t = x, x = y, y = t, 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 + +; }    }}voidCalheight (int*r,intN) {    intI, j, k =0;  for(i =1; I <= N; i + +) rk[sa[i]] =i;  for(i =0; I < n; Height[rk[i + +]] =k) {         for(K. K--:0, j = sa[rk[i]-1]; R[i+k] = = R[j+k]; K + +); }}voidinit () {memset (SA,0,sizeof(SA)); memset (RK,0,sizeof(RK)); memset (height,0,sizeof(height)); memset (WA,0,sizeof(WA)); memset (WB,0,sizeof(WB)); memset (WV,0,sizeof(WV)); memset (WD,0,sizeof(wd));}BOOLCmppintXinty) {     for(intI=0; i<n;i++)    {        if(s[0][x+i]!=s[1][y+i]) {returns[0][x+i]>s[1][y+H]; }    }    returnx<=n-y-1;}intMain () {intT; scanf ("%d",&T); CharSTR[MAXN];  while(t--)    {        intF1,f2; scanf ("%d",&N); scanf ("%s", str);        Init ();  for(intI=0; i<n;i++) s[0][i]=s[0][i+n]= (int) Str[i]; NN=n<<1; s[0][nn]=0; Build_sa (s[0],nn+1, $);  for(inti=nn;i>=1; i--)        {            if(sa[i]<N) {F1=Sa[i];  Break;        }} init ();  for(intI=0; i<n;i++) s[1][i]=s[1][i+n]= (int) str[n-i-1]; s[1][nn]=0; Build_sa (s[1],nn+1, $); Calheight (s[1],nn); intmx=N; F2=-1; //for (int i=1;i<=nn;i++) cout<<i<< "" <<sa[i]<< "" <         for(inti=nn;i>=1; i--)        {            if(f2!=-1) Mx=min (mx,height[i+1]); if(mx<n) Break; if(sa[i]<N) {F2=Sa[i]; }        }     //cout<<f1<< "" <<f2<<endl;        if(CMPP (F1,F2)) {printf ("%d 0\n", f1+1); }        Else{printf ("%d 1\n", N-F2); }    }}

Hdu 5442 favorite Donut 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.