hdoj5442Favorite Donut【后缀数组】

Favorite Donut

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2770    Accepted Submission(s): 677


Problem 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  ith  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,T20 , which means the number of test case.

For each test case, the first line contains one integer  n,n20000 , 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
 

Source
 

题意:给你一个字符串组成环,每次可以选取一个字符顺时针展开这个字符串或者逆时针展开这个字符串问其中字典序最大的字符串的起始位置和旋转方向,如果有相同的优先选择起始位置较小的如果有相同则优先选择顺时针。

解题思路:分别求两次后缀数组即可。求出后缀数组sa之后从后向前扫当用height数组判断当两个后缀的前缀小于n即跳出。

/* ***********************************************
Author       : ryc
Created Time : 2016-08-08 Monday
File Name    : E:\acm\hdoj\5442.cpp
Language     : c++
Copyright 2016 ryc All Rights Reserved
************************************************ */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
const int maxn=200010;
char s[maxn];
int sa[maxn],t1[maxn],t2[maxn],c[maxn];
int Rank[maxn],height[maxn];
void getHeight(int n){
    int k = 0;
    for(int i=1;i<=n;i++)Rank[sa[i]] = i;
    for(int i=0;i<n;i++){
        if(k)k--;
        int j = sa[Rank[i]-1];
        while(s[i+k]==s[j+k])k++;
        height[Rank[i]] = k;
    }
}
bool cmp(int *r,int a,int b,int l){
    return (r[a]==r[b] && r[a+l]==r[b+l]);
}
void build_sa(int m,int n){
    int i,*x=t1,*y=t2,k,p;
    for( i=0;i<m;i++)c[i] = 0;
    for( i=0;i<n;i++)c[x[i] = s[i]]++;
    for( i=1;i<m;i++)c[i] += c[i-1];
    for( i=n-1;i>=0;i--)sa[-- c[x[i]]] = i;
    for(k=1,p=0;p<n;m=p,k<<=1){
        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++)c[i] = 0;
        for(i=0;i<n;i++)c[x[y[i]]]++;
        for(i=1;i<m;i++)c[i] += c[i-1];
        for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]] = y[i];
        swap(x,y);
        p = 1; x[sa[0]] = 0;
        for(i=1;i<n;i++)
            x[sa[i]] = cmp(y,sa[i-1],sa[i],k)?p-1:p++;
    }
    getHeight(n-1);
}
struct Node{
    int pos;
    char str[maxn];
};
char str[maxn],s1[maxn];
int main()
{
    int t;cin>>t;
    while(t--){
        int n,len=0;cin>>n;
        scanf("%s",str);
        strcpy(s,str);strcat(s,str);
        build_sa(255,2*n+1);
        int pos,dir;Node ans1,ans2;
        ans1.pos=maxn;ans2.pos=0;
        ans1.str[0]=ans2.str[0]=0;height[2*n+1]=n;
        for(int i=2*n;i>=1;--i){
            if(sa[i]<n){
                if(height[i+1]<n)break;
                ans1.pos=min(sa[i]+1,ans1.pos);
            }
        }
        for(int j=ans1.pos,cnt=0;cnt<n;cnt++,j++){
            ans1.str[cnt]=s[j];
        }ans1.str[n]=0;
        for(int i=n-1;i>=0;--i){
            s[n-i-1]=str[i];
        }
        for(int i=n,cnt=n-1;cnt>=0;++i,cnt--){
            s[i]=str[cnt];
        }s[2*n]=0;
        build_sa(255,2*n+1);height[2*n+1]=n;
        for(int i=2*n;i>=1;--i){
            if(sa[i]<n){
                if(height[i+1]<n)break;//小于n说明前面的一定小于当前的字符串
                //printf("%d ",sa[i]+1);
                ans2.pos=max(ans2.pos,sa[i]+1);
                //printf("%d ",ans2.pos);
            }
        }
        for(int j=ans2.pos,cnt=0;cnt<n;++j,++cnt){
            ans2.str[cnt]=s[j];
        }ans2.str[n]=0;ans2.pos=n-ans2.pos+1;
        if(strcmp(ans1.str,ans2.str)==0){
            if(ans1.pos<=ans2.pos)
                printf("%d %d\n",ans1.pos,0);
            else
                printf("%d %d\n",ans2.pos,1);
        }
        else if(strcmp(ans1.str,ans2.str)>0){
            printf("%d %d\n",ans1.pos,0);
        }
        else {
            printf("%d %d\n",ans2.pos,1);
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值