hdu 4333 Revolving Digits

本文针对HDU 4333 RevolvingDigits问题提供了解决方案。该问题要求计算通过循环移动数字能产生的不同整数数量,包括小于、等于及大于原始数的整数。文章详细介绍了使用KMP算法确定循环节的方法,并通过扩展KMP实现进一步计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Revolving Digits

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

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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

 

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:   4332  4335  4334  4337  4338
 
题意:输出字符串的同构字符串中,比给出字符串小、等于、大于的个数
相同字符串只能算一次
 
先用kmp求循环节,计算相同字符串
再两个串接起来套扩展kmp模板
 
#include<cstdio>
#include<iostream>
#include<cstring>
#define N 200100
using namespace std;

int nxt[N],cas,len;
char T[N];
int f[N];

void kmp()
{
    int j;
    for(int i=1;i<len;i++)
    {
        j=f[i];
        while(j && T[i]!=T[j]) j=f[j];
        f[i+1]= T[i]==T[j] ? j+1 : 0;
    }
}
void getnxt()
{
    int a=0,Tlen=strlen(T);
    nxt[0]=Tlen;
    while(a<Tlen-1 && T[a]==T[a+1]) a++;
    nxt[1]=a;
    a=1;
    for(int k=2;k<Tlen;k++)
    {
        int p=a+nxt[a]-1,L=nxt[k-a];
        if(k-1+L>=p)
        {
            int j=(p-k+1>0) ? p-k+1 : 0;
            while(k+j<Tlen && T[k+j]==T[j]) j++;
            nxt[k]=j;
            a=k;
        }
        else nxt[k]=L;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",T);
        len=strlen(T);
        kmp();
        int k=len-f[len],tt;
        if(len%k==0) tt=len/k;
        else tt=1;
        for(int i=0;i<len;i++) T[len+i]=T[i];
        getnxt();
        int num1=0,num2=0,num3=0;
        for(int i=0;i<len;i++)
        {
            if(nxt[i]>=len) num2++;
            else if(T[nxt[i]]>T[i+nxt[i]]) num1++;
            else num3++;
        }
        printf("Case %d: %d %d %d\n",++cas,num1/tt,num2/tt,num3/tt);
    }
}

 

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/7043820.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值