Revolving Digits

Revolving Digits

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


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
 

题意:给你一个数字,把最后一个数字放到最前面去,经过几次变换后又回到原数字,问在这些数字中,比原数字小的,相等的,大的分别有多少个,去除重复的

扩展kmp寻找移位前和移位后的字符串要比较的唯一确定的位置。  去除重复的数字用kmp找到循环节

代码如下:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
const int N=100500;
char s[2*N],t[N];
int ext[N],extand[N];
int slen,tlen,len;
int get_len(char t[])
{
    int i=0,j=-1;
    ext[0]=-1;
    while(i<tlen)
    {
        if(j==-1||t[i]==t[j])
            ext[++i]=++j;
        else
            j=ext[j];
    }
    return tlen-ext[tlen];
}
void getnext(char T[]){// next[i]: 以第i位置开始的子串 与 T的公共前缀
     int i,length = strlen(T);
     ext[0] = length;
     for(i = 0;i<length-1 && T[i]==T[i+1]; i++);
          ext[1] = i;
          int a = 1;
          for(int k = 2; k < length; k++){
                  int p = a+ext[a]-1, L = ext[k-a];
                  if( (k-1)+L >= p ){
                       int j = (p-k+1)>0? (p-k+1) : 0;
                       while(k+j<length && T[k+j]==T[j]) j++;// 枚举(p+1,length) 与(p-k+1,length) 区间比较
                       ext[k] = j, a = k;
                  }
                  else ext[k] = L;
         }
}


void getextand(char S[],char T[]){
    memset(ext,0,sizeof(ext));
    getnext(T);
    int  a = 0;
    int MinLen = slen>tlen?tlen:slen;
    while(a<MinLen && S[a]==T[a]) a++;
    extand[0] = a, a = 0;
    for(int k = 1; k < slen; k++){
        int p = a+extand[a]-1,L= ext[k-a];
        if( (k-1)+L >= p )
        {
            int j = (p-k+1)>0? (p-k+1) : 0;
            while(k+j<slen && j<tlen && S[k+j]==T[j] ) j++;
            extand[k] = j;a = k;
        }
        else extand[k] = L;
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",t);
        strcpy(s,t);
        strcat(s,t);
        slen=strlen(s);
        tlen=strlen(t);
        len=get_len(t);//循环节
        if(tlen%len!=0)
            len=tlen;
        getextand(s,t);
        int s1(0),s2(0),s3(0);
        for(int j=0;j<len;j++)
        {
            if(extand[j]>=tlen)//也可以不用判断 s2一定为1
                s2++;
            else if(s[extand[j]+j]<s[extand[j]])
                s1++;
            else if(s[extand[j]+j]>s[extand[j]])
                s3++;
        }
        printf("Case %d: ",i);
        printf("%d %d %d\n",s1,s2,s3);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值