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<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#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<len;i++)
     {
          j=next[i];
          while(j&&p[i]!=p[j])
               j=next[j];
          if(p[i]==p[j])
               next[i+1]=j+1;
            else
               next[i+1]=0;
     }
}

int getmin()   //最小表示法
{
     int i=0,j=1,k=0;
     while(i<len1&&j<len1&&k<len1)
     {
          if(p1[i+k]==p1[j+k])
               k++;
          else if(p1[i+k]<p1[j+k])
          {
               j=j+k+1;
               k=0;
          }
          else
          {
               i=i+k+1;
               k=0;
          }
          if(i==j)
              j++;
     }
     return i;
}

int getmax()   //最大表示法
{
     int i=0,j=1,k=0;
     while(i<len1&&j<len1&&k<len1)
     {
          if(p1[i+k]==p1[j+k])
               k++;
          else if(p1[i+k]<p1[j+k])
          {
               i=i+k+1;
               k=0;
          }
          else
          {
               j=j+k+1;
               k=0;
          }
          if(i==j)
              j++;
     }
     return i;
}

int main()
{
     while(~scanf("%s",p))
     {
          len=strlen(p);
          strcpy(p1,p);
          strcat(p1,p);
          getnext();
          cnt=1; //记录循环的次数,利用最小循环节的知识
          if(len%(len-next[len])==0)
             cnt=len/(len-next[len]);
          len1=len/cnt;    //只考虑循环节以前的即可
          printf("%d %d %d %d\n",getmin()+1,cnt,getmax()+1,cnt);
     }
     return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值