hdu 5442 Favorite Donut 后缀数组

Favorite Donut

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

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 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

First line contain one integer 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 lowercase alphabets representing the ring donut.

Output

You should print one line for each test case, consisted of two 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

 

题意

给你一个环,环上的字符表示甜的程度,你可以正着吃,也可以反着吃

然后问你怎么吃可以使得甜度最大

题解:

后缀数组,正反各跑一遍,然后拿到俩长度为n的最大的字符串

然后再o(n)比一下就好了

正解其实应该是最小表示法,十几行就可以解决,O(n)

大概后缀自动机也是O(n)的算法也可以搞定~

代码:

//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>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 50000
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

int s[2][maxn];
int n,nn;
int sa[maxn], rk[maxn], height[maxn];
int wa[maxn], wb[maxn], wv[maxn], wd[maxn];

int cmp(int *r, int a, int b, int l){
    return r[a] == r[b] && r[a+l] == r[b+l];
}

void build_sa(int *r, int n, int m){
    int i, 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 = n-1; i >= 0; i --) sa[-- wd[x[i]]] = i;
    for(j = 1, p = 1; p < n; j *= 2, m = p){
        for(p = 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 = n-1; 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 ++;
        }
    }
}

void calHeight(int *r, int n){
    int i, 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 ++);
    }
}
void init()
{
    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));
}
bool cmpp(int x,int y)
{
    for(int i=0;i<n;i++)
    {
        if(s[0][x+i]!=s[1][y+i])
        {
            return s[0][x+i]>s[1][y+i];
        }
    }
    return x<=n-y-1;
}
int main()
{
    int T;
    scanf("%d",&T);
    char str[maxn];
    while(T--)
    {
        int f1,f2;
        scanf("%d",&n);
        scanf("%s",str);
        init();
        for(int i=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,200);
        for(int i=nn;i>=1;i--)
        {
            if(sa[i]<n)
            {
                f1=sa[i];
                break;
            }
        }
        init();
        for(int i=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,200);
        calHeight(s[1],nn);
        int mx=n;
        f2=-1;
     //   for(int i=1;i<=nn;i++) cout<<i<<" "<<sa[i]<<" "<<height[i]<<endl;
        for(int i=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);
        }
    }
}

 

    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/4805288.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞