Revolving Digits(kmp扩展,最小循环数去重+最长后缀)

Revolving Digits
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25029 Accepted Submission(s): 5452

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

/*
题目意思是给出一串字符数,你通过移动分别找出比原来数小的,相等的,比他大的三种数的数量个数,一般我们是从最左往最右移动,但这次是从最右往最左移动,所以要反过来看。
题解:1.将字符串复制一串加在原串的后面,然后每次记录它的当前后缀,比较后缀与该后缀串的最长公共前缀,当公共前缀>=len时,显然相等,否则只要比较下一位就能确定这个串与原串的大小关系。
2.去重,用最短循环数,len%i?len:i,在该值里面寻找,减少时间if(nextt[i]>len)则相等个数+1if(0<nextt[i]<len)—>且s[i+nextt[i]]>s[nextt[i]]则比他大个数+1,除此以外的情况都是比它小,还有就是如果nextt[i]=0时,判断当前s[i]是否大于s[0],大于就是比他大个数+1,不然就是比他小个数+1
*/
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 2e5+10;
char s[N*2];
int nextt[N*2];
int n,m,len;
/*void get_next(char *s,int *n)
{
    int i=0,k=-1;
    nextt[0]=-1;
    while(i<len)
    {
        if(k==-1||s[i]==s[k])
        {
            i++,k++;
            nextt[i]=k;
        }
        else
            k=nextt[k];
    }
}*/
/*

    扩展KMP 求出一个串所有后缀串(即s[i...len])和模式串的最长公共前缀。
于是只要将这个串复制一遍,求出该串每个后缀与其本身的最长公共前缀即可,
当公共前缀>=len时,显然相等,否则只要比较下一位就能确定这个串与原串的大小关系。
    至于重复串的问题,只有当这个串有循环节的时候才会产生重复串,
用KMP的next数组求出最小循环节,用长度除以最小循环节得到循环节个数,
在将3个答案都除以循环节个数即可。
*/
/*void get_next(char *s,int *nextt)
{
    int i=0;
    int len=strlen(s);
    nextt[0]=len;
    while(i<len-1&&s[i]==s[i+1])
        i++;
    i=1;
    for(int j=2; j<len; j++)
    {
        int p=i+nextt[i]-1,L=nextt[j-i];
        if(L>=p-j+1)
        {
            int k=max(p-j+1,0);
            while(i+j<len&&s[j+k]==s[k])
                k++;
            nextt[j]=k;
            i=j;
        }
        else
            nextt[j]=L;
    }
}*/
void getNext(char *s,int *nextt)
{
    int len=strlen(s);
    int i=0;
    nextt[0]=len;
    while(i<len-1&&s[i]==s[i+1])//去重
        i++;
    nextt[1]=i;
    i=1;
    for(int j=2; j<len; j++)//kmp扩展,当前后缀与该后缀的公共前缀
    {
        int p=i+nextt[i]-1;//初始i的nextt值
        if(j+nextt[j-i]-1>=p)
        {
            int k=max(p-j+1,0);//next[0.....k....i - 1]中最大的k即k+nextt[k]
            while(j+i<len&&s[j+k]==s[k])
                k++;
            nextt[j]=k;
            i=j;
        }
        else
            nextt[j]=nextt[j-i];
    }

}
int main()
{
    int n;

    cin >> n;
    getchar();
    for(int m = 1; m<=n; m++)
    {
        gets(s);
        int len = strlen(s);
        for(int j = 0; j<len; j++)
        {
            s[len+j] = s[j];
        }
        s[2*len] = '\0';
        getNext(s,nextt);
        int l = 0,e = 0,g = 0;
        int k;
        for(int i = 1; i<=len; i++)//去重
        {
            if(i+nextt[i]>=len)
            {
                k = len%i?len:i;
                break;
            }
        }
        for(int i = 0; i<k; i++)//
        {
            if(nextt[i] >= len)
                e++;
            else if(nextt[i] > 0 && nextt[i]<len)
            {
                if(s[i+nextt[i]]>s[nextt[i]])
                    g++;
                else
                    l++;
            }
            else if(nextt[i] == 0)
            {
                if(s[i]>s[0])
                    g++;
                else
                    l++;
            }
        }
        printf("Case %d: %d %d %d\n",m,l,e,g);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值