HDU 4333 Revolving Digits 2012多校リーグ第4戦C題(拡張KMP+KMP)


Revolving Digits
Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1140    Accepted Submission(s): 332
Problem Description
One day Silence is interested in revolving the digits of a positive integer. In the revolving operation, he can put several last digits to the front of the integer. Of course, he can put all the digits to the front, so he will get the integer itself. For example, he can change 123 into 312, 231 and 123. Now he wanted to know how many different integers he can get that is less than the original integer, how many different integers he can get that is equal to the original integer and how many different integers he can get that is greater than the original integer. We will ensure that the original integer is positive and it has no leading zeros, but if we get an integer with some leading zeros by revolving the digits, we will regard the new integer as it has no leading zeros. For example, if the original integer is 104, we can get 410, 41 and 104.
 
Input
The first line of the input contains an integer T (1<=T<=50) which means the number of test cases. 
For each test cases, there is only one line that is the original integer N. we will ensure that N is an positive integer without leading zeros and N is less than 10^100000.
 
Output
For each test case, please output a line which is "Case X: L E G", X means the number of the test case. And L means the number of integers is less than N that we can get by revolving digits. E means the number of integers is equal to N. G means the number of integers is greater than N.
 
Sample Input

   
   
   
   
1 341

 
Sample Output

   
   
   
   
Case 1: 1 1 1

 
                   
   
数をあげますが、串で処理する必要があります.ループシフトは最後に頭になるまで移動し、最初の数より小さい数が何個あるかを尋ね、同じ理屈でより大きい数を求める.
              问题の考え方:その时最大最小表示法をやっていて、それからこの问题を见て直接そこに行きました.しかし、最大最小表示は中で最小の戻り値を探しているだけで、2つのポインタが指しているだけです.降りてから問題解を見て、拡張KMPで作りました.拡張KMPは、1つの列のすべての接尾辞列(すなわち、s[i...len])とモード列の最長共通接頭辞を求めることができる.前は少し見ましたが、勉強がうまくできなかったので、テーマも書けませんでした.拡張KMPは簡単だと思います.この列を一度コピーして、その列の各接尾辞とそれ自体の最長の共通接頭辞を求めればいいです.共通接頭辞>=len/numの場合、明らかに等しいです.そうしないと、次の列と元の列の大きさの関係を比較すれば、この列と元の列の大きさの関係を確定することができます.そしてnumはKMP最小サイクル節で与えられるに違いない.
              タイトルアドレス:Revolving Digits
ACコード:
/*
       KMP
        (a a c a d e f g a a c a) *2
   extend   24 1 0 1 0 0 0 0 4 1 0 2
   entend[8]=4;  entend[9],  i           ,  extend[i-k]  
*/
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define MAXN 1000005
using namespace std;
int next[MAXN],extend[MAXN],len1,len2;
char p[MAXN],s[MAXN*2];

void getnext()
{
    int i,j;
    next[0]=0,next[1]=0;
    for(i=1;i<len1;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;
    }
}

void getextend()
{
    int i=1,k=0;
    extend[0]=len2;
    while(k+1<len2&&s[k]==s[k+1]) k++;
    extend[1]=k;
    k=1;
    for(i=2;i<len1;i++)
    {   //       /num  
        int p=k+extend[k]-i;   // i          
        if(p<0) p=0;
        extend[i]=p;
        if(extend[i-k]<p) extend[i]=extend[i-k];
        // i-k        
        while(i+extend[i]<len2&&s[extend[i]]==s[i+extend[i]])
               extend[i]++;
        if(i+extend[i]>k+extend[k])
              k=i;
    }
}

int main()
{
    int T,cas,i;
    scanf("%d",&T);
    for(cas=1;cas<=T;cas++)
    {
        scanf("%s",p);
        len1=strlen(p),len2=len1*2;
        strcpy(s,p);
        strcat(s,p);
        s[len2]='\0';
        int cnt1=0,cnt2=0,cnt3=0;  //             

        getnext();
        getextend();
        /*for(i=0;i<len1;i++)
             cout<<extend[i]<<" ";
        cout<<endl;*/
        int num=1;
        if(len1%(len1-next[len1])==0) num=len1/(len1-next[len1]);  //num       
        len2=len2/num,len1=len1/num;
        for(int i=0;i<len1;i++)
        {
            if(extend[i]>=len1)cnt2++;
            else
            {
                if(s[i+extend[i]]<p[extend[i]])
                    cnt1++;
                else cnt3++;
            }
        }
        printf("Case %d: %d %d %d
",cas,cnt1,cnt2,cnt3); } return 0; }