HDU4333[Revolving Digits]

来源:互联网 发布:vs2015写c语言 编辑:程序博客网 时间:2024/06/05 19:13

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


solution: 扩展kmp

#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;#define N 100010char t[N*2];int nxt[N], lent, lens;void get_nxt(){    int i=0, j=-1;    nxt[0]=-1;    while( i<lent ){        if( j==-1||t[i]==t[j] ) nxt[++i]=++j;        else j=nxt[j];    }}void get_ext(){    int a=0, i=1;    nxt[0]=lent;    while( a+1<lens && t[a]==t[a+1] )++a;    nxt[1]=a;    a=1;    while( ++i<lent ){        int mx= a+nxt[a]-1;        nxt[i]=min( nxt[i-a], max(0,mx-i+1) );        while( i+nxt[i]<lens && t[nxt[i]]==t[i+nxt[i]]) ++nxt[i];        if( i+nxt[i]>a+nxt[a] ) a=i;    }}int main(){    int T, num=0;    scanf("%d",&T);    while( T-- ){        scanf("%s", t);        lent=strlen(t);        lens=lent+lent;        get_nxt();        int tmp=lent%(lent-nxt[lent])==0 ? lent/(lent-nxt[lent]):1;        for ( int i=0; i<=lent; i++ ) t[i+lent]=t[i];        get_ext();        int a,b,c;        a=b=c=0;        for ( int i=0; i<lent; i++ ) if( nxt[i]>=lent ) b++;            else if ( t[nxt[i]]<t[i+nxt[i]] ) c++;            else a++;         cout<<"Case "<<++num<<": "<<a/tmp<<' '<<b/tmp<<' '<<c/tmp<<endl;      }}