HDU 4333

B - Revolving Digits
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 4333
Appoint description:
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

题目很好理解,但是这题卡的是时间。如果暴力的话直接TLE

就是用 EXKMP做

下面附上AC代码

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <string.h>
#define maxs 10^100000*2
using namespace std;
char s1[2*maxs];
int nexts[maxs];
int big,les,equ;
void get_nexts(char *s,int *n)
{
    int len=strlen(s),i,p,L,j,a=0;
    n[0]=len;
    while(a<len-1&&s[a]==s[a+1])
        a++;
    n[1]=a;
    a=1;
    for(int k=2; k<len; k++)
    {
        p=a+nexts[a]-1,L=nexts[k-a];
        if((k-1)+L>=p)    // 这里错了   不知道为什么   k-a-1+L
        {
            j=(p-k+1)>0?p-k+1:0;
            while(k+j<len&&s[k+j]==s[j])
                j++;
            n[k]=j;
            //if(j>p)
            a=k;
        }
        else nexts[k]=L;
    }
    /*for(int i=0;i<=len;i++)
    {
        printf("i is %d n[i] is %d\n",i,nexts[i]);
    }*/
}
int main()
{
    int t;
    scanf("%d",&t);
    getchar();
    int times=1;
    while(t--)
    {
        equ=big=les=0;
        gets(s1);
        int len=strlen(s1);
        for(int i=0; i<len; i++)
        {
            s1[i+len]=s1[i];
        }
        s1[2*len]='\0';
        //printf("%s\n",s1);
        get_nexts(s1,nexts);
        int k;
        for(int i=1; i<=len; i++)
        {
            if((i+nexts[i])>=len)
            {
                k=len%i?len:i;
                break;
            }
        }
        //printf("k is %d\n",k);
        for(int i=0; i<k; i++)
        {
             if(nexts[i]>=len)
                 equ++;
            else if(nexts[i]>0&&s1[i+nexts[i]]>s1[nexts[i]])
                 big++;
             else if(nexts[i]>0&&s1[i+nexts[i]]<s1[nexts[i]])
                 les++;
             else if(nexts[i]==0)
             {
                 if(s1[i]>s1[0])
                    big++;
                else if(s1[i]<s1[0])
                    les++;
            }
        }
         // printf("k is %d\n",k);
         printf("Case %d: %d %d %d\n",times++,les,equ,big);
    }
    return 0;
}

你可能感兴趣的:(HDU,exkmp)