HDU 4333 Revolving Digits(EX_KMP)

来源:互联网 发布:电脑下载iphone软件 编辑:程序博客网 时间:2024/04/28 16:55
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
1341
 

Sample Output
Case 1: 1 1 1
 

Source
2012 Multi-University Training Contest 4
 
分析:先找出来最小循环节,然后ex_kmp求后缀和串的匹配长度
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int maxn=1e5+100;char str[maxn*2];int extend[maxn*2];int net[maxn*2];void get_next(int len){    int i=0,k,j;    extend[0]=len;    while(i<len-1&&str[i]==str[i+1]) ++i;    extend[1]=i;i=1;    for(k=2;k<len;k++)    {        int p=i+extend[i]-1,l=extend[k-i];        if(k-1+l>=p)        {            j=max(p-k+1,0);            while(k+j<len&&str[k+j]==str[j]) ++j;            extend[k]=j;            i=k;        }        else            extend[k]=l;    }}void get(int len){    int i=0,j=-1;    net[0]=-1;    while(i<len)    {        if(j==-1||str[i]==str[j])            net[++i]=++j;        else            j=net[j];    }}int main(){    int t,cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%s",str);        int len=strlen(str);        int L=0,E=0,G=0;        for(int i=0;i<len;i++)            str[i+len]=str[i];        str[len*2]=0;        get_next(len*2);        get(len*2);        int h=len*2-net[len*2];//循环节        for(int i=0;i<h;i++)        {            if(extend[i]>=len)                ++E;            else if(extend[i]>0&&extend[i]<len)            {                if(str[i+extend[i]]>str[extend[i]])                    ++G;                else                    ++L;            }            else if(extend[i]==0)            {                if(str[i]>str[0])                    ++G;                else                    ++L;            }        }        printf("Case %d: %d %d %d\n",cas++,L,E,G);    }    return 0;}


0 0