Revolving Digits

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

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
 
  这道题就是求一个数字循环移位后和原数字大小比对结果的计数,求小于原数的,等于原数的,大于原数的不同的由原数循环移位得到的数的个数。
  由于每个位置只有一个数,考虑对于每个位置O(1)出解。那么对于比较大小的性质,可以发现:一定会有头一段相等,然后接着开始不同,所以可以求LCP。
  然后可以用后缀数组,把原数复制一遍拼接到后面,找到原数的位置,再枚举每个循环移位后的位置,用RMQ求出LCP,然后统计答案;这里我用的扩展KMP,一样求LCP,但感觉更适合这道题,比后缀数组完美些。
  然而还没完,因为要找出不同的数,上面的方法可能重复计数了,再思考其性质,发现若一个数被计K次,那么其他所有的数都被计了K次,又可以发现K是原字串的循环数,再用普通KMP解决就好了。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int lens,lent,Q,p,cas;
int f[maxn],r[maxn];
char s[maxn],t[maxn];
struct ExKmp{
void Init(){
memset(f,,sizeof(f));
memset(r,,sizeof(r));
s[lens+]='%';t[lent+]='&';
} void Get_Fail(){
f[]=lent;
while(t[+f[]]==t[+f[]])
f[]+=;p=;
for(int i=;i<=lent;i++){
int k=p+f[p]-,L=f[i-p+];
if(i+L-<k)f[i]=L;
else{
f[i]=max(,k-i+);
while(t[+f[i]]==t[i+f[i]])
f[i]+=;p=i;
}
}
} void Exkmp(){
Init();Get_Fail();
while(s[+r[]]==t[+r[]])
r[]++;p=;
for(int i=;i<=lens;i++){
int k=p+r[p]-,L=f[i-p+];
if(i+L-<k)r[i]=L;
else{
r[i]=max(,k-i+);
while(t[+r[i]]==s[i+r[i]])
r[i]+=;p=i;
}
}
}
}kmp; int fail[maxn];
int Get_Rnd(){
fail[]=fail[]=;
for(int i=;i<=lent;i++){
int j=fail[i];
while(j!=&&t[i]!=t[j])j=fail[j];
fail[i+]=t[i]==t[j]?j+:;
}
if(lent-fail[lent]==||lent%(lent-fail[lent]))
return ;
else
return lent/(lent-fail[lent]);
} void Solve(){
kmp.Exkmp();
int rnd=Get_Rnd();
int ans1=,ans2=,ans3=;
for(int i=;i<=lent;i++){
if(r[i]==lent)ans2++;
else if(s[i+r[i]]<t[r[i]+])ans1++;
else if(s[i+r[i]]>t[r[i]+])ans3++;
}
printf("%d %d %d\n",ans1/rnd,ans2/rnd,ans3/rnd);
} int main(){
scanf("%d",&Q);
while(Q--){
scanf("%s",t+);
lent=strlen(t+);
for(int i=;i<=lent;i++)
s[i]=s[lent+i]=t[i];
printf("Case %d:",++cas);
lens=lent*;Solve();
}
return ;
}

字符串(扩展KMP):HDU 4333 Revolving Digits的更多相关文章

  1. 扩展KMP - HDU 4333 Revolving Digits

    Revolving Digits Problem's Link Mean: 给你一个字符串,你可以将该字符串的任意长度后缀截取下来然后接到最前面,让你统计所有新串中有多少种字典序小于.等于.大于原串. ...

  2. HDU 4333 Revolving Digits 扩张KMP

    标题来源:HDU 4333 Revolving Digits 意甲冠军:求一个数字环路移动少于不同数量 等同 于的数字 思路:扩展KMP求出S[i..j]等于S[0..j-i]的最长前缀 推断 nex ...

  3. HDU 4333 Revolving Digits 扩展KMP

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意:给以数字字符串,移动最后若干位到最前边,统计得到的数字有多少比原来大,有多少和原来同样,有多少 ...

  4. 【扩展kmp+最小循环节】HDU 4333 Revolving Digits

    http://acm.hdu.edu.cn/showproblem.php?pid=4333 [题意] 给定一个数字<=10^100000,每次将该数的第一位放到放到最后一位,求所有组成的不同的 ...

  5. HDU - 4333 Revolving Digits(扩展KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意 一个数字,依次将第一位放到最后一位,问小于本身的数的个数及等于本身的个数和大于本身的个数,但是要注意 ...

  6. HDU 4333 Revolving Digits [扩展KMP]【学习笔记】

    题意:给一个数字,每一次把它的最后一位拿到最前面,一直那样下去,分别求形成的数字小于,等于和大于原来数的个数. SAM乱搞失败 当然要先变SS了 然后考虑每个后缀前长为n个字符,把它跟S比较就行了 如 ...

  7. HDU - 4333 Revolving Digits(拓展kmp+最小循环节)

    1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> ...

  8. HDU 4333 Revolving Digits

    扩展KMP的应用 我们发现本题的关键在于如何高效的判断两个同构字符串的大小关系,想到如果能够预处理出每一个同构字符串与原字符串的最长公共前缀,那么直接比较它们不一样的部分就好,扩展KMP正好是用来处理 ...

  9. Hdu 4333 Revolving Digits(Exkmp)

    Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. rabbitmq 消息持久化之receive and send

    二: 任务分发 &消息持久化   启用多个接收端的时候如果某一个receive 关闭要保证消息有反馈是否收到   send端 #-*- coding: UTF-8 -*-import pika ...

  2. MyTask1

    从去年10月份开始着手的一个小项目,啊喂~人家更笨就没学过.net好不,都是现学现卖的丫~~~23333,好了好了,下面进入正题: 陆陆续续做了很长时间(其实也就是这两天兴趣说来就来,许久没码代码手痒 ...

  3. 玩javaweb的web.xml编译路径

    有时候能够碰到这样的情况 缓存就是 清不掉 那就可以去寻找编译路径了 <Context docBase="E:\java-workspace\eigyo_com405" pa ...

  4. ASP。net中如何在一个按钮click事件中调用另一个按钮的click事件

    方法一: 直接指定 事件<asp:Button ID="btn1" runat="server" Text="按钮1" onclick ...

  5. C#和asp.net中链接数据库中 参数的几种传递方法

    #region 参数传递方法第一种 //参数设置方法(第一种) //SqlParameter sp = new SqlParameter("@Name", str_Name); / ...

  6. 进程识别号(PID)的理解

    PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID. PID(进程控制符)英文全称为Process ...

  7. App上线基本流程

    还可参考的:http://www.cocoachina.com/bbs/read.php?tid=330302 iOS项目上传前期准备材料: 1.已有开发者账号 2.已有发布证书 3.一张1024*1 ...

  8. Uploadify 笔记分享 -- 2014年10月18日

    最近要做一个项目,有个部分需要用到Uploadify,以前用过,但不是很懂,找了无数遍的中文文档,发现好多都是以前的,都不能用,一时间索性自己写了个笔记,随用随查 <form> <i ...

  9. [转]PHP取整函数:ceil,floor,round,intval的区别详细解析

    我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval. 1.ceil -- 进一法取整 说明float ceil ( float value ) 返回不小于 value ...

  10. JavaScript中style.left与offsetLeft的区别

    今天在制作焦点轮播图的时候,遇到一个问题,在使用style.left获取图片的位置时,怎么也获取不到.换用offsetLeft就能够成功获取到了.虽然实现了我想要的效果,但是还是不甘心啊,没有找到原因 ...