hdu 4333 扩展kmp

Revolving Digits

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1165    Accepted Submission(s): 351


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
 

Source
2012 Multi-University Training Contest 4
比赛的时候题读错了,这题就是,要找出一个字符串所拼成了很多数字,与目标数字比较得出大于小于等于的个数,我们,可以想到,要找最长公共前缀,当然是扩展kmp,这里,我们可以知道,如果有重复的数字,我们只要找到最小周期,在这一个小周期中,就可以得出个数了!其实,我们可以发现,i+next[i]就是重复子串的长度!这样,用个循环找到最大的长度就要以了!
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
#define MAXN 100000
char pass[MAXN],str[MAXN];
int next[MAXN],extend[MAXN];
int strnum,passnum;
int  fmax(int a,int b){if(a>b)return a;return b;}
int  fmin(int a,int b){if(a<b)return a;return b;}
void getnext()
{
    int i,k,j,p,l;
    next[0]=passnum;
    i=0;
    while(i<passnum-1&&pass[i]==pass[i+1])i++;
    next[1]=i;
    i=1;
    for(k=2;k<passnum;k++)
    {
        j=i+next[i]-1;p=next[k-i];
        if(k+p-1>=j)
        {
            l=fmax(0,j-k+1);
            while(l+k<passnum&&pass[l]==pass[k+l])l++;
            i=k;
            next[k]=l;
        }
        else
        {
            next[k]=p;
        }
    }
}


int  main ()
{
    int i,l,e,g,tcase,tt;
    tcase=1;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%s",pass);
        passnum=strlen(pass);
        strnum=2*passnum;
        for(i=passnum;i<strnum;i++)
        {
            pass[i]=pass[i-passnum];
        }
        str[i]='\0';
        passnum=2*passnum;
        getnext();
        int k;
        for(i=1;i<=passnum;i++)//求出最小循环节
		{
			if(i+next[i]>=passnum)
			{
				k=passnum%i?passnum:i;
				break;
			}
		}
		int less=0;
		int e=0;
		int m=0;
		for(i=0;i<k;i++)
		{
			if(next[i]>=passnum)
				e++;
			else if(next[i]>=0)
			{
				if(pass[i+next[i]]>pass[next[i]])
					m++;
				else less++;
			}
		}
		printf("Case %d: %d %d %d\n",tcase++,less,e,m);


    }
    return 0;
}


你可能感兴趣的:(hdu 4333 扩展kmp)