HDU 5442 Favorite Donut(最大表示法)

来源:互联网 发布:网络营销策划什么意思 编辑:程序博客网 时间:2024/05/22 04:59

Favorite Donut

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


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 ofn 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 theith 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 are2n 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 ofn 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 (from1 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
 

大体题意:
给你一个长度为n 的字符串,你有2n种方式构造这个字符串,就是从某一个位置开始逆时针或者顺时针走,求出字典序最大的方案并且求出位置,如果正反字典序相同,输出位置小的那个!
思路:
用最小表示法模板改造成最大表示法,顺时针很好说。
逆时针的话,虽然反转了字符串,但位置也反转了,所以有个小小的改动:
        if (k >= n){
            i = i + (n-1-i)/(j-i)*(j-i);
            break;
        }
详细见代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<iostream>using namespace std;int ans[2];const int maxn = 20000 + 10;string solve1(string s,int id){    int i,j,k,l;    int n = s.length();    s+=s;    for (i = 0, j = 1; j < n; ){        for (k = 0; k < n && s[i + k] == s[j + k]; ++k);        if (k >= n)break;        if (s[i+k] > s[j+k])            j += k+1;        else {            l = i + k;            i = j;            j = max(j,l) + 1;        }    }    ans[id] = i;    return s.substr(i,n);}string solve2(string s,int id){    int i,j,k,l;    int n = s.length();    s += s;    for (i = 0, j = 1; j < n; ){        for (k = 0; k < n && s[i+k] == s[j+k]; ++k);\        if (k >= n){            i = i + (n-1-i)/(j-i)*(j-i);            break;        }        if (s[i+k] > s[j+k]){            j += k+1;        }        else{            l = i + k;            i = j;            j = max(j,l)+1;        }    }    ans[id] = i;    return s.substr(i,n);}char s[maxn];int main(){    int T;    scanf("%d",&T);    while(T--){        int n;        scanf("%d",&n);        scanf("%s",s);        string s1 = solve1(s,0);        reverse(s,s+n);        string s2 = solve2(s,1);        int ans1 = ans[0],ans2 = ans[1];        if (s1 > s2){            printf("%d %d\n",ans1+1,0);        }        else if (s1 < s2){            printf("%d %d\n",n-ans2,1);        }        else{            int pos1 = ans1 + 1;            int pos2 = n-ans2;            if (pos1 <= pos2)printf("%d %d\n",ans1+1,0);            else printf("%d %d\n",n-ans2,1);        }    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 儿子18岁谈朋友怎么办 和朋友为钱吵架怎么办 感情里总担心失去怎么办 眼石移出盲僧怎么办 打仗把小便踢肿了怎么办 腿上都是挠的疤怎么办 脚上的肉烂了怎么办 商铺门口有电杆怎么办 漏电保护器坏了怎么办 空开进线烧了怎么办 过压保护灯亮怎么办 美的热水器接地异常怎么办 欠压保护器坏了怎么办 三孔插座没地线怎么办 二胡琴筒裂缝宽怎么办 有了月亮从四星宠满级的怎么办 党委下属没有党支部了党委怎么办 发生日期大于制单日期怎么办 美的空调出现p0怎么办 薯片受潮不脆了怎么办 泡过的莲子煮不烂怎么办 绿豆有煮不熟的怎么办 吃了羊肉吃西瓜怎么办 吃了狗肉和绿豆怎么办 做的衣柜没有门怎么办 蒸馒头熟了会瘪怎么办 3dmax贴图太大了怎么办 嘴皮边缘颜色深怎么办 嘴巴周围肤色暗沉怎么办 中奖彩票被洗了怎么办 牙龈下面长米粒肉疙瘩怎么办 书画印章盖反了怎么办 金龙鱼一个月不吃东西怎么办 罗汉鱼头撞扁了怎么办 房顶开槽埋线白色不一样怎么办 顶上灯挪位置线怎么办 马蜂窝弄掉又来怎么办 蜂窝弄掉又有怎么办 2018年小龙虾底板脏怎么办 一本分数线擦边过怎么办 玩具塑料球扁了怎么办