字符串(扩展KMP):HDU 4333 Revolving Digits
Revolving Digits
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24729 Accepted Submission(s): 5381
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.
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.
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.
#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的更多相关文章
- 扩展KMP - HDU 4333 Revolving Digits
Revolving Digits Problem's Link Mean: 给你一个字符串,你可以将该字符串的任意长度后缀截取下来然后接到最前面,让你统计所有新串中有多少种字典序小于.等于.大于原串. ...
- HDU 4333 Revolving Digits 扩张KMP
标题来源:HDU 4333 Revolving Digits 意甲冠军:求一个数字环路移动少于不同数量 等同 于的数字 思路:扩展KMP求出S[i..j]等于S[0..j-i]的最长前缀 推断 nex ...
- HDU 4333 Revolving Digits 扩展KMP
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意:给以数字字符串,移动最后若干位到最前边,统计得到的数字有多少比原来大,有多少和原来同样,有多少 ...
- 【扩展kmp+最小循环节】HDU 4333 Revolving Digits
http://acm.hdu.edu.cn/showproblem.php?pid=4333 [题意] 给定一个数字<=10^100000,每次将该数的第一位放到放到最后一位,求所有组成的不同的 ...
- HDU - 4333 Revolving Digits(扩展KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意 一个数字,依次将第一位放到最后一位,问小于本身的数的个数及等于本身的个数和大于本身的个数,但是要注意 ...
- HDU 4333 Revolving Digits [扩展KMP]【学习笔记】
题意:给一个数字,每一次把它的最后一位拿到最前面,一直那样下去,分别求形成的数字小于,等于和大于原来数的个数. SAM乱搞失败 当然要先变SS了 然后考虑每个后缀前长为n个字符,把它跟S比较就行了 如 ...
- HDU - 4333 Revolving Digits(拓展kmp+最小循环节)
1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> ...
- HDU 4333 Revolving Digits
扩展KMP的应用 我们发现本题的关键在于如何高效的判断两个同构字符串的大小关系,想到如果能够预处理出每一个同构字符串与原字符串的最长公共前缀,那么直接比较它们不一样的部分就好,扩展KMP正好是用来处理 ...
- Hdu 4333 Revolving Digits(Exkmp)
Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- lucene 抛出的异常(分享)
1) too many boolean clauses异常 例如: String keyword=".......";//(keyword的长度太长) Query indexQue ...
- maven jetty运行命令
1.先运行build.xml <?xml version="1.0" encoding="UTF-8"?> <project name=&qu ...
- angularJS随笔
1.作用域 基于作用域的事件传播 作用域可以像DOM节点一样,进行事件的传播.主要是有两个方法: broadcasted :从父级作用域广播至子级 scope emitted :从子级作用域往上发射到 ...
- 表达式:使用API创建表达式树(5)
一.ConditionalExpression:表达式 生成如 IIF((a == b), "a和b相等", "a与b不相等") 式子. 使用: Paramet ...
- C#开发基于Http的LaTeX数学公式转换器
本文将讲解如何通过codecogs.com和Google.com提供的API接口来将LaTeX数学函数表达式转化为图片形式.具体思路如下: (1)通过TextBox获取用户输入的LaTeX数学表达式, ...
- 监听指定端口数据交互(HttpListenerContext )
很怀念以前做机票的日子,,,,可惜回不去 以前的项目中的,拿来贴贴 场景:同步第三方数据,监听指定地址(指定时间间隔,否则不满足,因为需要处理粘包问题,改篇未实现) 主要内容四个文件:下面分别说下每个 ...
- Node中Exports与module.export的使用与区别
最近在看<node开发实战详解>时有写疑问,所以自己就整理了一些资料.下面是node4.*的官方api文档(http://nodejs.cn/doc/node_4/modules.html ...
- 关于get和set访问器以及属性和字段变量的区别问题
属性是对一个或者多个字段的封装. 类里面为什么要用一个共有的属性来封装其中的字段,也可以这样说用共有属性来封装私有变量,其中的好处应该大家都说的出来,就是为了实现数据的封装和保证了数据的安全 ...
- 使用Xcode插件,让iOS开发更加便捷
在iOS开发过程中,写注释是一项必不可少的工作.这不仅有助于自己对代码整理回顾,而且提高了代码的可读性,让代码维护变得容易.但是,写注释又是一项枯燥的工作.我们浪费了大量的时间在输入/*,*,*/这样 ...
- Jquery 点击空白处消失
$(document).bind("click", function (e){ if ( $((e.target || e.srcElement)).closest("# ...