Revolving Digits   (扩展KMP)

来源:互联网 发布:淘宝微商代理能挣钱吗? 编辑:程序博客网 时间:2024/05/22 08:26

http://acm.hdu.edu.cn/showproblem.php?pid=4333

Revolving Digits

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

题目大意:

给一个数字字符串S,  可以把S最后一个数字移动到最前面变成另一个数字。例如123,  经过移动依次变成312,231,123。 注意当移动次数正好和S长度相等时,S又变回了最开始的那个数字。

求这个移动过程所形成的所有字符串,大于S(最初的)的数字,等于S,以及小于S的各有多少个,这些字符串中若有重复的,不能累计。

分析与总结:

若有循环节,则必会有重复字符串,否则没有;设Str=1234;令s=12341234 (将Str重复一次再赋给s),令t=1234;s中有5个连续相邻的四位字符串,除去最后一个,前四个每个都代表一种移位后的结果。用扩展KMP寻找移位前和移位后的字符串要比较的唯一确定的位置。

#include<stdio.h>#include<iostream>#include<string.h>using namespace std;const int maxn=100009;char str[maxn],s[maxn*2];int next[maxn*2];int extend[maxn*2];int L,E,G;void ekmp(){    int i,j,p,l;    int lent=strlen(str);    int lens=2*lent;    next[0]=lent;    j=0;    while(j+1<lent&&str[j]==str[j+1])j++;    next[1]=j;    int a=1;    for(i=2;i<lent;i++)    {        p=next[a]+a-1;        l=next[i-a];        if(i+l<p+1)next[i]=l;        else        {            j=max(0,p-i+1);            while(j+i<lent&&str[i+j]==str[j])j++;            next[i]=j;            a=i;        }    }    j=0;    while(j<lens&&j<lent&&s[j]==str[j])j++;    extend[0]=j;    a=0;    for(i=1;i<lent;i++) //不必i<lens,i<lent足矣    {        p=extend[a]+a-1;        l=next[i-a];        if(l+i<p+1)extend[i]=l;        else        {            j=max(0,p-i+1);            while(i+j<lens&&j<lent&&s[i+j]==str[j])j++;            extend[i]=j;            a=i;        }    }}void ans(){    int i,j;    int n=strlen(str);    for(i=0;i<n;i++)    {        if(i!=0&&extend[i]>=n&&n%i==0)break;//判断循环节        if(extend[i]>=n)E++;        else        {            if(s[i+extend[i]]>str[extend[i]])G++;             else L++;        }    }}int main(){    int t,cas=1;    scanf("%d",&t);    while(t--)    {        L=E=G=0;        scanf("%s",str);        strcpy(s,str);        strcat(s,str);        ekmp();        ans();        printf("Case %d: %d %d %d\n",cas++,L,E,G);    }    return 0;}


原创粉丝点击