hdu 5442 Favorite Donut 最小表示法+KMP 2015长春网络赛

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

Favorite Donut

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


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
 


题意:

有一个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  找其中 相同串的 最大起始位置。 


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

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


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <deque>
#include <set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
typedef long long LL;
const int INF = 1<<10;
const LL mod = 95041567; 

//最小表示法
int len;
int get_mstring(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);
}
char s[21000],revs[21000];
char now1[21000],now2[21000];




//KMP
#define N 20010   //子链长度
#define M 40010 //母链长度
char a[M], b[N];
int Next[N];



void getNext(char s[], int len)
{
    int i, j;
    i = 0; j = -1;
    Next[0] = -1;
    while (i < len)
    {
        if (j == -1 || s[i] == s[j])
            ++i, ++j, Next[i] = j;
        else
            j = Next[j];
    }
} 
 
int ans;
int KMP(const char a[], const char b[], int pos, int next[], int la, int lb) 
{
    int i, j;
    i = pos, j = 0;
    next[0] = -1; 
    while (i < la)
    {
        if (j == -1 || a[i] == b[j])
            ++i, ++j;
        else
            j = next[j];
		if (j >= lb) 
		{
			if(i<2*len)  //这边不能 有 -1
			{
				ans=min(ans,    len-(  i+1-len) +1); 
			} 
			j=next[j]; //如果不能子链重合 这边要改成j=0;
		}
    }
    return ans;
} 
 

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&len);
        scanf("%s",s);


        for(int i=0;i<len;i++)
            revs[i]=s[len-1-i];
        revs[len]=0;


	//	cout<<"fan"<<revs<<endl;
        int l0=get_mstring(s);//下标
	    for(int i=0;i<len;i++)
            now1[i]=s[(i+l0)%len];
		now1[len]=0;


		//cout<<"zheng zuixiao   "<<l0<<endl;
		
		 
		for(int i=0;i<len;i++)
			a[i]=a[len+i]=revs[i];
		a[2*len]=0;

		int l2=get_mstring(revs);
		for(int i=0;i<len;i++)
            now2[i]=revs[(i+l2)%len];
		now2[len]=0;  
		


		l0++;
		l2=ans=len-(l2+1)+1;

		int dx=strcmp(now1,now2);
		if(dx<0)
		{
			getNext(now2,len); 
			KMP(a, now2, 0, Next, 2*len, len); 
			 
			printf("%d 1\n",ans); 

		}
		else if(dx>0)
		{
			printf("%d 0\n",l0);
		}
		else //liang zhe xiangtong  kan xia biaol   yong kmp  chuli chu a de xiabiao
		{
			getNext(now2,len); 
			KMP(a, now2, 0, Next, 2*len, len); 

			if(ans<l0)
				printf("%d 1\n",ans);
			else
				printf("%d 0\n",l0); 
		}  
    }
    return 0;
}
/*
44
4
abcd
5
aabbaa

7
abcdcba

1
a

2
aa

*/







你可能感兴趣的:(KMP,最小表示法)