【HDU4333】Revolving Digits(扩展KMP+KMP)
Revolving DigitsDescription
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
341Sample Output
Case 1: 1 1 1
【题意】
给定一个数字<=10^100000,一次将该数的第一位放到放到最后一位,求所有组成的不同的数比原数小的个数,相等的个数,大的个数。
【分析】
扩展KMP和KMP,KMP用来求循环节。
扩展KMP部分的代码(求next):
void exkmp()
{
nt[1]=2*l;int mx=0,id;
while(mx+1<=2*l&&s[mx+1]==s[mx+2]) mx++;
nt[2]=mx;id=2;
for(int i=3;i<=2*l;i++)
{
mx=id+nt[id]-1;
int now=nt[i-id+1];
if(i+now<=mx) nt[i]=now;
else
{
int j=mx-i+1;
if(j<0) j=0;
while(i+j<=2*l&&s[i+j]==s[1+j]) j++;
nt[i]=j;id=i;
}
}
}
KMP部分代码(求next):

(注意红色部分)
啦啦啦啦
本题代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 100010 char s[*Maxn];
int l; int nt[*Maxn];
void exkmp()
{
nt[]=*l;int mx=,id;
while(mx+<=*l&&s[mx+]==s[mx+]) mx++;
nt[]=mx;id=;
for(int i=;i<=*l;i++)
{
mx=id+nt[id]-;
int now=nt[i-id+];
if(i+now<=mx) nt[i]=now;
else
{
int j=mx-i+;
if(j<) j=;
while(i+j<=*l&&s[i+j]==s[+j]) j++;
nt[i]=j;id=i;
}
}
} int ntt[Maxn*]; void kmp()
{
ntt[]=;int p=;
for(int i=;i<=l;i++)
{
while((s[i]!=s[p+]&&p)||p>=l) p=ntt[p];
if(s[i]==s[p+]&&p+<=l) p++;
ntt[i]=p;
}
} int main()
{
int T,kase=;
scanf("%d",&T);
while(T--)
{
scanf("%s",s+);
l=strlen(s+);
for(int i=;i<=l;i++) s[i+l]=s[i];
exkmp();
int a1=,a2=,a3=;
for(int i=;i<=l;i++)
{
if(nt[i]>=l) a2++;
else if(s[i+nt[i]]<s[+nt[i]]) a1++;
else a3++;
}
int x=;
kmp();
if(l%(l-ntt[l])==) x=l/(l-ntt[l]);
printf("Case %d: ",++kase);
printf("%d %d %d\n",a1/x,a2/x,a3/x);
}
return ;
}
[HDU4333]
2016-08-19 14:44:52
【HDU4333】Revolving Digits(扩展KMP+KMP)的更多相关文章
- hdu4333 Revolving Digits(扩展kmp)
Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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 -> ...
- [hdu4333]Revolving Digits
/*注意注意:本题非hdu4333原题,而是简化版,原版有多组数据.但此代码在修改输入后依旧可以通过多组数据*/ 给出一个数字串,问有多少本质不同同构串比原串小,一样,大.同构串是指,对于原串S[1- ...
- 字符串(扩展KMP):HDU 4333 Revolving Digits
Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 扩展KMP - HDU 4333 Revolving Digits
Revolving Digits Problem's Link Mean: 给你一个字符串,你可以将该字符串的任意长度后缀截取下来然后接到最前面,让你统计所有新串中有多少种字典序小于.等于.大于原串. ...
- HDU - 4333 :Revolving Digits (扩展KMP经典题,问旋转后有多少个不同的数字小于它本身,等于它本身,大于它本身。)
One day Silence is interested in revolving the digits of a positive integer. In the revolving operat ...
- HDU 4333 Revolving Digits 扩张KMP
标题来源:HDU 4333 Revolving Digits 意甲冠军:求一个数字环路移动少于不同数量 等同 于的数字 思路:扩展KMP求出S[i..j]等于S[0..j-i]的最长前缀 推断 nex ...
随机推荐
- Linux Bash终端支持中文显示
方法:修改系统变量LANG 即时生效: LANG=en_US.UTF- 永久生效: 修改.bashrc,加入 fi
- 通过ApplicationContextAwareSpring实现手工加载配置的javabean
在做一个多线程的数据采集器实现的过程中,由于框架是集成srping,因此希望统一使用原有的数据库配置信息,但是需要手工获取数据库配置bean.我们可以通过继承ApplicationContextAwa ...
- c语言学习之基础知识点介绍(二十):预处理指令
一.预处理指令的介绍 预处理命令:在编译之前触发的一系列操作(命令)就叫预处理命令. 特点:以#开头,不要加分号. #include: 文件包含指令 把指定文件的内容复制到相应的位置 #define: ...
- UIView的常见属性
UIView的常见属性: @interface UIView : UIResponder<NSCoding, UIAppearance, UIAppearanceContainer, UIDyn ...
- OC - 5.内存管理
一.引用计数器 1> 栈和堆 栈 ① 主要存储局部变量 ② 内存自动回收 堆 ① 主要存储需要动态分配内存的变量 ② 需要手动回收内存,是OC内存管理的对象 2> 简介 作用 ① 表示对象 ...
- call与apply的区别
/** * 用 add 来替换 sub,add.call(sub,3,1) == add(3,1) ,所以运行结果为:alert(4); */ function add(a,b){ alert(a+b ...
- 页面资源预加载(Link prefetch)功能加速你的页面加载速度
有了浏览器缓存,为何还需要预加载? 用户可能是第一次访问网站,此时还无缓存 用户可能清空了缓存 缓存可能已经过期,资源将重新加载 用户访问的缓存文件可能不是最新的,需要重新加载 页面资源预加载/预读取 ...
- 04_XML_03_XMLDTD约束与校验
[什么是XML约束] 在XML技术里,可以编写一个文档来约束一个XML文档的书写规范,称之为XML约束. 常见的XML约束技术有:XML DTD和XML Schema DTD(Document Typ ...
- 【转】PHP网站常见安全漏洞,及相应防范措施总结
---恢复内容开始--- 目前,基于PHP的网站开发已经成为目前网站开发的主流,本文笔者重点从PHP网站攻击与安全防范方面进行探究,旨在减少网站漏洞,希望对大家有所帮助! 一.常见PHP网站安全漏洞 ...
- mui H5 js动态添加不同类型的数据
html页面需要添加的页面的数据格式 <ul class="mui-table-view" id="OA_task_1"> <li class ...