HDU 3374String Problem(最大最小表示法+KMP)

String Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1292    Accepted Submission(s): 581


Problem Description
Give you a string with length N, you can generate N strings by left shifts. For example let consider the string “SKYLONG”, we can generate seven strings:
String Rank 
SKYLONG 1
KYLONGS 2
YLONGSK 3
LONGSKY 4
ONGSKYL 5
NGSKYLO 6
GSKYLON 7
and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once.
  Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.
 

Input
  Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.
 

Output
Output four integers separated by one space, lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), the string’s times in the N generated strings, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.
 

Sample Input
 
   
abcder aaaaaa ababab
 
Sample Output
 
   
1 1 6 1 1 6 1 6 1 3 2 3
 


                       题目大意:循环移位,一直移到尾变成头,中间的出现的情况,在里面找字典序最前的和字典序最后的下标标号,然后把次数也输出来。

               解题思路:很容易想到次数肯定是用最小循环节求出来,然后就是如何找出字典序最小的,一直在纠结这个地方。开始将最大最小表示法神化了,当时比赛碰到那个题多校联赛2012第四场的C题,也不敢下手。其实最大最小表示法原来就是暴力。。。而我优化了一点,是因为在找字典序最小最大的时候,只需要找编号在最小循环节以前的即可,代码中的len1.详解见代码。

                题目地址:String Problem

AC代码:
#include
#include
#include
#include
#define MAX 1000005
using namespace std;
char p[MAX];
char p1[MAX*2];  //把模式串复制一份
int len,next[MAX],cnt,len1;

void getnext()
{
     int i,j;
     next[0]=0,next[1]=0;
     for(i=1;i


你可能感兴趣的:(KMP)