HDU 5442——Favorite Donut——————【最大表示法+kmp | 后缀数组】

Favorite Donut

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


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
2
4
abab
4 aaab
 

 

Sample Output
2 0
4 0
 

 

Source
 
 
题目大意:顺时针或逆时针遍历字符串,让求最大字典序。如果最大字典序唯一,那么输出得到最大字典序的位置及遍历顺序。如果不唯一,那么位置小的优先,如果位置也相同,那么顺时针优先。结果输出位置及遍历顺序。顺时针为0,逆时针为1。
 
吐槽:自己被自己玩死了。数据范围自己开小了,题目是2W,自己yy的1W,错到死  WA。
 
解题思路:对于顺时针得到最大字典序的最小下标,可以用最大表示法直接得到。对于逆时针的最小下标,我们首先将原字符串反转,然后用最大表示法得到下标,但是得到的这个下标,并不是原串逆序得到最大字典序的最小下标,而是最大的。所以我们将反转后的串复制在另外一个空串中,再在尾部连上一个反转后的串,将这个长度为2n的串作为文本串。然后我们在反转后的串中提取出在原串中逆序最大字典序的串作为模式串,然后用kmp匹配,这时候匹配需要得到最大的匹配位置(由于是反转后的串,所以最大即在原串中最小)。最后先比较正序和逆序得到的字符串是否相同,不同取大的遍历顺序;相同则比较位置大小,小的或等的顺时针优先。
 
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+500;
char str[maxn],sp[maxn],revs[maxn],rvsp[maxn];
char tx[3*maxn];
int f[maxn];
int get_max(char *s){   //最大(最小)表示法得到的是正序遍历最大(最小)字典序开始位置。
    int i=0,j=1,k=0;
    int len=strlen(s);
    while(i<len&&j<len){
        if(k==len){
            break;
        }
        if(s[(i+k)%len]<s[(j+k)%len]){
            i=i+k+1>j? i+k+1:j+1;
            k=0;
        }else if(s[(i+k)%len]>s[(j+k)%len]){
            j=j+k+1>i? j+k+1:i+1;
            k=0;
        }else{
            k++;
        }
    }
    return min(i,j);
}
void get_s_p(char *sr,char *s,int st,int len){  //提取字符串
    for(int i=st,k=0;k<len;i++,k++){
        sr[i-st]=s[i%len];
    }
    sr[len]='\0';
}
void rev(char *s,int len){  //反转
    for(int i=len-1;i>=0;i--){
        revs[len-1-i]=s[i];
    } revs[len]='\0';
}

void getfail(char *P,int *F){
    int m=strlen(P);
    F[0]=F[1]=0;    int j;
    for(int i=1;i<m;i++){
        j=F[i];
        while(j&&P[i]!=P[j]) j=F[j];
        F[i+1]=P[i]==P[j]?j+1:0;
    }
}
int kmp(char *T,char *P){
    int n=strlen(T),m=strlen(P);
    int j=0;
    int ret=0;
    for(int i=0;i<n-1;i++){
        while(j&&P[j]!=T[i]) j=f[j];
        if(P[j]==T[i]) j++;
        if(j==m){
            ret=max(ret,i-m+1);     //对于反转后的串,需得到最大位置
            j=f[j];
        }
    }
    return ret;
}
int main(){
    int t , n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        scanf("%s",str);
        rev(str,n);

        int ckw=get_max(str);
        get_s_p(sp,str,ckw,n);

        int ctckw=get_max(revs);
        get_s_p(rvsp,revs,ctckw,n);

        strcpy(tx,revs);
        strcat(tx,revs);

        getfail(rvsp,f);
        int ans=kmp(tx,rvsp);   //在反转后的串中的位置
        ans=n-1-ans;    //转化为原串中的位置
        int d=strcmp(sp,rvsp);
        if(d>0){
            printf("%d 0\n",ckw+1);
        }else if(d<0){
            printf("%d 1\n",ans+1);
        }else{
            if(ckw<=ans){
                printf("%d 0\n",ckw+1);
            }else{
                printf("%d 1\n",ans+1);
            }
        }
    }
    return 0;
}

  

 
 
 

转载于:https://www.cnblogs.com/chengsheng/p/4818495.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
hao123网址站ASP开源源码正式发布 本程序源码由hao123.com网址站独立开发,更新hao123最新样式和分类,asp版带后台,可全站生成静态页。 关于网站LOGO修改: 首页网站标志LOGO目录位置:/images/logo_140.gif 次页网站标志LOGO目录位置:/logo.gif 希望大家创建有自己特色的LOGO,以区别于hao123。 默认后台地址:/admin/login.asp 默认用户:admin 默认密码: admin 关于网站安装: 1, 上传源码,进入后台->全局->站点信息,把信息修改成你站信息 2, 生成首页和所有分类HTML 建议上传后修改默认管理后台地址/admin/的名称。 演式地址:http://www.86daijia.net 2012-5-16 增加站长统计程序及图标。 2012-5-16 美化界面。 2012-5-16 增加最新新闻资讯。 2012-5-16 修正后台管理全局设置不能保存BUG。 2012-4-9 增加IP购买页面。 2012-4-9 增加分享工具条。 2012-4-9 增加下角话费充值条 2012-4-9 支持二级目录,为站长节省空间 2012-2-27 再次与123同步 2011-11-7 新增折扣分类。 2011-10-24 同步hao123.com首页时间、天气的显示方式。 2011-10-14 新增实用工具、彩票、音乐、小游戏聚合分类。 2011-9-30 重新整理了分类。 2011-5-8 增加电影分类,自动调用wz123.net电影数据。修复军事、旅游、酷站链接名。 2011-4-2 增加连续剧分类,自动调用hao123.com连续剧数据。 2011-4-1 改进天气自动调用weather.com.cn数据。 2011-1-10 增加设计、微博分类,同步分类排序。 2011-1-10 同步天气预报数据,自动获取IP显示城市天气。 2010-11-8 增加笑话分类,同步分类排序。 2010-11-7 修改163与126邮箱登录自动跳转官网,修复子目录返回首页链接。 2010-10-20 最新同步团购分类。 2010-7-28 最新同步hao123.com清新绿风格。 2010-3-5 应广大站长需要,再次提供HTML版本下载, 2010-1-15 同步hao123分类顺序,最新更新wz123.net职业与爱好分类。 2010-1-11 最新修复网站放在子目录上栏目页样式问题,有此问题的用户请下载最新版覆盖修复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值