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. 45种Javascript技巧大全

    JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发(Node.js和Wakanda)等等.JavaScript还是 ...

  2. WCF 用netTcpbinding,basicHttpBinding 传输大文件

    问题:WCF如何传输大文件 方案:主要有几种绑定方式netTcpbinding,basicHttpBinding,wsHttpbinding,设置相关的传输max消息选项,服务端和客户端都要设置,tr ...

  3. .NET Core跨平台开发

    对于.NET开源计划想必关注的人已经跃跃欲试了,但是真正将其用于开发的目前来说不多.毕竟截至本文发布时.NET Core才发布到1.0RC2版本.正式版预计还有一段时间.况且大多数人都是持观望态度,就 ...

  4. 40个DBA日常维护的SQL脚本--1113

    from itpub --1.查询碎片程度高的表--条件为什么block>100,因为一些很小的表,只有几行数据实际大小很小,但是block一次性分配就是5个(11g开始默认一次性分配1M的bl ...

  5. Java发邮件:Java Mail与Apache Mail

    作者:Vinkn 来自http://www.cnblogs.com/Vinkn/ 一.邮件简介 一封邮件由很多信息构成,主要的信息如下,其他的暂时不考虑,例如抄送等: 1.收件人:收件人的邮箱地址,例 ...

  6. 谷歌地图实现车辆轨迹移动播放(google map api)

    开发技术:jquery,js baidu map api,json,ajax QQ1310651206 谷歌地图(google map api)实现车辆轨迹移动播放(google map api)

  7. 通过C# 打开一个应用程序

    System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo(); //设置外部程序名 Info ...

  8. WordPress防暴力破解:安全插件和用.htpasswd保护WordPress控制面板

    正在用Wordpress的博主们一定知道最近全球兴起的一波黑客锁定Wordpress暴力破解控制面板密码的风波了,据CloudFlare执行长Matthew Prince所说,所谓的暴力密码攻击是输入 ...

  9. iOS适配:Masonry介绍与使用实践:快速上手Autolayout

    随着iPhone的手机版本越来越多, 那么对于我们广大的开发者来说就是很悲催,之前一直使用代码里面layout的约束来适配, 现在推荐一个第三方Masonry,上手块,操作简单,只能一个字形容他 “爽 ...

  10. CreateJS第0章- Canvas基础

    最近网页游戏比较火,以前做过一些小游戏,但是过段时间就都忘了,今天在这里记录一下学习过程,以备后用.做网页游戏有很多种框架,我是flash程序用Adobe出品的CreateJS最容易.基本上继承了fl ...