HDU 5442 Favorite Donut(最大表示法 + KMP 2015长春网络赛)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5442


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

题意:

贴一发队友的题解:http://blog.csdn.net/u013532224/article/details/48417723

题意:

有一个len长度的环,问有没有  最大的  长度为 len 的 串在 这个环里。

如果有的话,且只有一个 ,输出其 开头的下标, 下标从1 开始, 再输出0 表示 顺时针  1 表示逆时针

如果多个,输出 开头下标最小的那个。

如果 还有 ,就是顺时针 逆时针 一样的情况  输出 下标  0。


做法:

很容易想到最小表示法

        else
        {
            if(t>0)
                j+=k+1;
            else i+=k+1;    这里的 i  和 上一行的 j 交换就是 最小表示法模版了。
            if(i==j) j++;     
            k=0;
        }

然后 正序直接用最大表示法 ,可以得到  最大字典序,最小下标的 开头位置。

逆序 就把串反下,用最大表示法,可以得到  最大字典序,  也是下标最小的 开头位置,  但是因为颠倒了,  这里的下标是倒序的,

所以 下标其实是最大的。 但是我们已经得到 最大 字典序的串了,  所以 接下来  把反的串 重复一遍, 可以 用kmp  找其中 相同串的 最大起始位置。 


然后把 两个 最大字典序的串 比较下大小。

正序大输出 正序 逆序大 输出逆序,相等 则输出 标号最小的。

PS:

例如

8
abcdabcd

这个案例,反过来后得到的串的 dcbadcba 我们需要把这个反串重复一遍,然后就是用模式串dcbadcba 去匹配总串 dcbadcba dcbadcba,然而第一次我们得到的KMP中的 i 是8(此处下标从1开始),ans = len1 - i ,len1是总串的长度(16 - 8)也是8,显然我们可以观察ans = 4 是最小的,那么我们继续向后面匹配,当匹配到i == 12的时候, ans = len1 - i (16 - 12 )

ans 等于4 就是我们要找的答案!然而我们不能再继续向后匹配了 再向后匹配最后得到的答案就变为0了,这是我们不愿看到的,

怎么办呢?

OK,我们只需要在KMP匹配的时候加一个限制条件 if(i < len1)才更新答案!


代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 20017;
char str[maxn], revs[maxn], trevs[2*maxn];
char a[maxn], b[maxn];
int Next[maxn];
int ans;
//最大表示法
int len;
int get_maxstring(char *s)
{
    int len = strlen(s);
    int i = 0, j = 1, k = 0;
    while(i<len && j<len && k<len)
    {
        int t=s[(i+k)%len]-s[(j+k)%len];
        if(t==0)
            k++;
        else
        {
            if(t > 0)
                j+=k+1;
            else
                i+=k+1;
            if(i==j) j++;
            k=0;
        }
    }
    return min(i,j);
}

void getnext( char T[],int len)
{
    int i = 0, j = -1;
    Next[0] = -1;
    while(i < len)
    {
        if(j == -1 || T[i] == T[j])
        {
            i++,j++;
            Next[i] = j;
        }
        else
            j = Next[j];
    }
}

void KMP(char s1[], char s2[], int len1, int len2 )//原串长度, 模式串长度
{
    int i = 0, j = 0 ;
    Next[0] = -1 ;
    while( i < len1 && j < len2 )
    {
        if( j == -1 || s1[i] == s2[j])
        {
            i++;
            j++;
        }
        else
            j = Next[j];
        if( j == len2)
        {
            if(i < len1)
            {
                ans = min(ans, len1 - i);
            }
            j = Next[j];
        }
    }
}

int main()
{
    int t;
    int n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        scanf("%s",str);
        int len = n;
        int min1 = get_mstring(str);
        for(int i = 0; i < len; i++)
        {
            revs[i] = str[len-i-1];
        }
        revs[len] = 0;
        int min2 = get_maxstring(revs);
        for(int i = 0; i < len; i++)
        {
            a[i] = str[(min1+i)%len];
        }
        a[len] = 0;
        min1++;//下标从1开始
        for(int i = 0; i < len; i++)
        {
            b[i] = revs[(min2+i)%len];
        }
        b[len] = 0;
        for(int i = 0; i < len; i++)
        {
            trevs[i] = trevs[i+len] = revs[i];
        }
        trevs[2*len] = 0;
        min2 = ans = len-(min2+1)+1;
        int tt = strcmp(a, b);
        if(tt > 0)
        {
            printf("%d 0\n",min1);
        }
        else
        {
            getnext(b, len);
            KMP(trevs, b, 2*len, len);
            if(tt < 0)
            {
                printf("%d 1\n",ans);
            }
            else if(tt == 0)
            {
                if(min1 > ans)
                {
                    printf("%d 1\n",ans);
                }
                else
                    printf("%d 0\n",min1);
            }
        }
    }
    return 0;
}
/*
99
4
abab
4
aaab
8
abcdabcd
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值