Favorite Donut


Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)




Problem Description


n parts. Every part has its own sugariness that can be expressed by a letter from a to z (from low to high), and a ring donut can be 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 two parts are equally sweet if they have the same sugariness.

Once Lulu eats a part of the donut, she must continue to eat its uneaten adjacent part until all parts are eaten. Therefore, she has to eat either clockwise or counter-clockwise after her first bite, and there are 2n ways to eat the ring donut of n parts. For example, Lulu has 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 way of the greatest lexicographic order. If there are two or more lexicographic maxima, then she will prefer the way whose starting part has the minimum index in clockwise order. If two ways start at the same part, then she will prefer eating the donut in clockwise order. Please compute the way to eat the donut she likes most.


 



Input


T,T≤20, which means the number of test case.

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


 



Output


1 to n) and the direction ( 0 for clockwise and 1


 



Sample Input


2 4 abab 4 aaab


 



Sample Output


2 0 4 0



题意:给出一个环,每颗珠子有一个甜度,选择第一个珠子和吃的方向,问得到的吃珠子的字符串的字典序最大的,如果有多个,选取位置最靠前的,如果还是多个,选择顺时针吃的。


解题思路:后缀数组,首先顺时针构造一个循环串,即将串复制一遍。再逆时针构造循环串,将两个串用'#'连在一起,和普通的后缀数组一样的处理方法。接着就是找sa[]里面最大的了,假设其为str,接下来就是找到其他串,它们有相同的公共前缀,且其前缀≥n,想想为什么?因为str肯定对应的是字典序最大的串,如果假设有串str',如果str'的串与str的公共前缀数小于n,说明str'不可能是字典序最大的。纸上画画就明白了。


#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>
using namespace std;
  
const int maxn = 200005;  
struct Node
{
	int pos,dir;
}ans[maxn];
int s[maxn<<2],t[maxn<<2],t2[maxn<<2];  
int Rank[maxn<<2],height[maxn<<2],sa[maxn<<2];  
int n,cnt,buc[maxn];  
char str[maxn];

bool cmp(Node a,Node b)
{
	if(a.pos == b.pos) return a.dir < b.dir;
	return a.pos < b.pos;
}
  
void getsa(int n,int m)  
{  
    int i,*x = t,*y = t2;  
    for(i = 0; i < m; i++) buc[i] = 0;  
    for(i = 0; i < n; i++) buc[x[i] = s[i]]++;  
    for(i = 0; i < m; i++) buc[i] += buc[i-1];  
    for(i = n-1; i >= 0; i--) sa[--buc[x[i]]] = i;  
    for(int k = 1; k <= n; k = k << 1)  
    {  
        int p = 0;  
        for(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++) buc[i] = 0;  
        for(i = 0; i < n; i++) buc[x[y[i]]]++;  
        for(i = 0; i < m; i++) buc[i] += buc[i-1];  
        for(i = n-1; i >= 0; i--) sa[--buc[x[y[i]]]] = y[i];  
        std::swap(x,y);  
        p = 1, x[sa[0]] = 0;  
        for(i = 1; i < n; i++)  
            x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k] ? p-1:p++;  
        if(p >= n) break;  
        m = p;  
    }  
}  
  
void getheight(int n)  
{  
    int i,j,k = 0;  
    for(i = 1; i <= n; i++) Rank[sa[i]] = i;  
    for(i = 0; i < n; i++)  
    {  
        if(k) k--;  
        if(Rank[i] == 0)  
        {  
            height[Rank[i]] = 0;  
            continue;  
        }  
        j = sa[Rank[i]-1];  
        while(s[i+k] == s[j+k]) k++;  
        height[Rank[i]] = k;  
    }  
}  

int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		scanf("%s",str);
		for(int i = 0; i < n; i++)
			s[i] = s[i+n] = str[i];
		s[2*n] = '#';
		for(int i = 0;i < n; i++)
			s[i+n*2+1] = s[i+n*3+1] = str[n-i-1];
		int len = 4 * n + 1;
		s[len+1] = 0;
		getsa(len+1,255);
		getheight(len);
/*		for(int i = 0; i <= len; i++)
			printf("%d %c\n",sa[i],s[sa[i]]);*/
		cnt = 0;
		ans[cnt++].pos = sa[len];
		for(int i = len-1; i >= 1; i--)
		{
			if(height[i+1] < n) break;
			if(sa[i] >= n && sa[i] <= 2*n) continue;
			if(sa[i] >= 3*n+1) continue;
			ans[cnt++].pos = sa[i];
		}
		for(int i = 0; i < cnt; i++)
		{
			if(ans[i].pos > 2*n) 
			{
				ans[i].dir = 1;
				ans[i].pos = n - (ans[i].pos - 2*n - 1);
			}
			else 
			{
				ans[i].dir = 0;
				ans[i].pos++;
			}
		}
		sort(ans,ans+cnt,cmp);
		printf("%d %d\n",ans[0].pos,ans[0].dir);
	}
	return 0;
}