• hdu5442 Favorite Donut


    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

    对于顺时针方向,可以直接直接用最小表示法算出最左边的位置,对于逆时针方向,要先把串倒过来,然后用二分查找出最右边的并且以该点为起点逆时针方向字典序最大的串,然后两者比较一下就行了。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define ll long long
    #define inf 0x7fffffff
    char s[20060],s1[20060],s2[20060],str[20060];
    int len;
    int minidx(char *s, int l)
    {
        int i = 0, j = 1, k = 0, t;
        while(i < l && j < l && k < l) {
            t = s[(i + k) >= l ? i + k - l : i + k] - s[(j + k) >= l ? j + k - l : j + k];
            if(!t) k++;
            else{
                if(t < 0) i = i + k + 1;
                else j = j + k + 1;
                if(i == j) ++ j;
                k = 0;
            }
        }
        return (i < j ? i : j);
    }
    
    int panduan(int x)
    {
        int i,j,weizhi;
        for(i=0;i<len;i++){
            str[i]=s[(x+i)%len];
        }
        weizhi=minidx(str,len);
        if(weizhi>len-1-x)return 0;
        return 1;
    
    }
    
    
    int main()
    {
        int n,m,i,j,T,fx,qidian,qidian1;
        int l,r,mid;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&len);
            scanf("%s",s);
            qidian=minidx(s,len);
            for(i=0;i<len;i++){
                s1[i]=s[(qidian+i)%len];
            }
            reverse(s,s+len);
            qidian1=minidx(s,len);
            for(i=0;i<len;i++){
                s2[i]=s[(qidian1+i)%len];
            }
    
            if(strcmp(s1,s2)>0){
                printf("%d 0
    ",qidian+1);continue;
            }
    
            l=qidian1;r=len-1;
            while(l<=r){
                mid=(l+r)/2;
                if(panduan(mid)){
                    l=mid+1;
                }
                else r=mid-1;
            }
            qidian1=r;
            for(i=0;i<len;i++){
                s2[i]=s[(qidian1+i)%len];
            }
            if(strcmp(s1,s2)<0){
                printf("%d 1
    ",len-qidian1);continue;
            }
            else if(qidian<=len-qidian1){
                printf("%d 0
    ",qidian+1);
            }
            else printf("%d 1
    ",len-qidian1);
    
        }
        return 0;
    }
    


  • 相关阅读:
    f(n)=1-1/2+1/3-1/4...+1/n
    练习2-15 求简单交错序列前N项和 (15 分)
    练习2-14 求奇数分之一序列前N项和 (15 分)
    练习2-13 求N分之一序列前N项和 (15 分)
    练习2-12 输出华氏-摄氏温度转换表 (15 分)
    【转载】如何从零开始开发一款嵌入式产品(20年的嵌入式经验分享学习,来自STM32神舟系列开发板设计师的总结
    【转载-Andrew_qian】stm32中断学习
    【转-Andrew_qian】stm32中断嵌套全攻略
    【转】写给自学者的入门指南
    【转】作为一个程序员,数学对你到底有多重要
  • 原文地址:https://www.cnblogs.com/herumw/p/9464663.html
Copyright © 2020-2023  润新知