hdu4333 扩展KMP

Revolving Digits

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


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
 
 
题目大意:给出一个数字,对这个数字进行操作:把最后一个放到最前面去。
求:进行完一轮后:有多少个  不同   的比它小,和它相等(包括自己),比它大的数
 
 
思路:
把原串复制一遍粘后面,然后进行一次扩展KMP求所有后缀与原串的最长公共前缀(方便比较大小),最后再做一个KMP去循环节(不同的数)
 
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>

int tt,t,x,z,q,e,i,j,n;
int s[300011],a[300011],next[300011];
char c;

using namespace std;

void Read()
{
    n=0;
    while(c=getchar(),c<'0'||c>'9');
    s[++n]=c;
    while(c=getchar(),c>='0'&&c<='9')s[++n]=c;
}

void Exkmp()
{
    int i,len,l,k;
    len=0;
    for(i=2;i<=n;i++){
        if(len<i){
            l=0;
            while(s[i+l]==s[1+l]&&i+l<=n)l++;
            a[i]=l;
        }
        else{
            l=a[i-k+1];
            if(l<len-i+1)a[i]=l;
            else{
                l=len-i+1;
                while(s[i+l]==s[1+l]&&i+l<=n)l++;
                a[i]=l;
            }
        }
        if(i+a[i]-1>len){
            len=i+a[i]-1;
            k=i;
        }
    }
    x=0;
    z=1;
    q=0;
    for(i=2;i<=n/2;i++){
        if(a[i]>=n/2)z++;
        else{
            if(s[i+a[i]]>s[1+a[i]])q++;
            if(s[i+a[i]]<s[1+a[i]])x++;
        }
    }
}

void Kmp()
{
    int i,x;
    memset(next,0,sizeof(next));
    x=0;
    for(i=2;i<=n/2;i++){
        while(x!=0&&s[i]!=s[x+1])x=next[x];
        if(s[i]==s[x+1])x++;
        next[i]=x;
    }
}

int main()
{
    scanf("%d",&t);
    for(tt=1;tt<=t;tt++){
        memset(s,0,sizeof(s));
        memset(a,0,sizeof(a));
        Read();
        for(i=1;i<=n;i++)s[i+n]=s[i];
        i+=n;
        n=n*2;
        Exkmp();
        Kmp();
        n=n/2;
        if(n%(n-next[n])==0){
            e=n/(n-next[n]);
            x/=e;
            z/=e;
            q/=e;
        }
        printf("Case %d: %d %d %d\n",tt,x,z,q);
    }
}

 

转载于:https://www.cnblogs.com/applejxt/p/3801784.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值