HDOJ 题目5442 Favorite Donut(后缀数组)

来源:互联网 发布:北斗卫星定位精度知乎 编辑:程序博客网 时间:2024/06/06 02:18

Favorite Donut

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


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 abcabc,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
24abab4aaab
 

Sample Output
2 04 0
 

Source
2015 ACM/ICPC Asia Regional Changchun Online
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5493 5492 5491 5490 5489 
 
也是比赛时没有读的题,下次不管做完做不完先把题都读了,,就是自己太看不起自己了,老去跟着被人,按通过数量做题,,,也不一定推的出来大哭这题很水啊,,虽然代码写的搓,但是也是明明可以做的啊哭
题目大意:就是给个串,然后0表示顺时针,1表示逆时针,找这个串里边含n个字符的串,让你按字典序->起始位置->查找方式(顺时针优先)的顺序判断,输出起始位置和查找方式
正反求两次就行了,因为我是按Rank值找的要保证起始位置最小,有个小的技巧的是正的算的时候最后赋值最小,反着建后缀数组是,最后付最大
ac代码

#include<string.h>              #include<algorithm>              #include<iostream>             #define min(a,b) (a>b?b:a)          #define max(a,b) (a>b?a:b)           using namespace std;            char str[150000],tmp[150000];          int sa[150000],Rank[150000],rank2[150000],height[150000],c[150000],*x,*y;        int n;        void cmp(int n,int sz)        {            int i;            memset(c,0,sizeof(c));            for(i=0;i<n;i++)                c[x[y[i]]]++;            for(i=1;i<sz;i++)                c[i]+=c[i-1];            for(i=n-1;i>=0;i--)                sa[--c[x[y[i]]]]=y[i];        }        void build_sa(char *s,int n,int sz)        {            x=Rank,y=rank2;            int i;            for(i=0;i<n;i++)                x[i]=s[i],y[i]=i;            cmp(n,sz);            int len;            for(len=1;len<n;len<<=1)            {                int yid=0;                for(i=n-len;i<n;i++)                {                    y[yid++]=i;                }                for(i=0;i<n;i++)                    if(sa[i]>=len)                        y[yid++]=sa[i]-len;                    cmp(n,sz);                swap(x,y);                x[sa[0]]=yid=0;                for(i=1;i<n;i++)                {                    if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len])                        x[sa[i]]=yid;                    else                        x[sa[i]]=++yid;                }                sz=yid+1;                if(sz>=n)                    break;            }            for(i=0;i<n;i++)                Rank[i]=x[i];        }        void getHeight(char *s,int n)        {            int k=0;            for(int i=0;i<n;i++)            {                if(Rank[i]==0)                    continue;                k=max(0,k-1);                int j=sa[Rank[i]-1];                while(s[i+k]==s[j+k])                    k++;                height[Rank[i]]=k;            }        } char str1[150050],str2[150050];int main(){int t;scanf("%d",&t);while(t--){//int n;scanf("%d",&n);scanf("%s",str);strcpy(tmp,str);int i;for(i=0;i<n;i++){str[n+i]=str[i];tmp[n+i]=str[i];}str[n+n]=0;build_sa(str,n+n,128);          getHeight(str,n+n+1);  int ansx=0;for(i=1;i<n;i++){if(Rank[i]>Rank[ansx])ansx=i;}int j=0;for(i=ansx;j<n;i++)str1[j++]=str[i];str1[j]='\0';for(i=0;i<n+n;i++){str[i]=tmp[n+n-i-1];}str[n+n]=0;build_sa(str,n+n+1,128);          getHeight(str,n+n); int ansy=n-1;for(i=n-2;i>=0;i--)if(Rank[i]>Rank[ansy])ansy=i;j=0;for(i=ansy;j<n;i++)str2[j++]=str[i];str2[j]='\0';ansy=n-ansy-1;//printf("%s %s\n",str1,str2);int ans=strcmp(str1,str2);//printf("%d %d\n",ansx,ansy);if(ans==0){if(ansx<=ansy)printf("%d 0\n",ansx+1,0);elseprintf("%d 1\n",ansy+1,1);}elseif(ans>0)printf("%d 0\n",ansx+1,0);elseprintf("%d 1\n",ansy+1,1);}}


0 0
原创粉丝点击