Revolving Digits

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

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
 

题目大意:把一个数的后面一位数字已到前面,如此循环统计比它本身小的,相等和比它大的数目。注意这样的数字必须是不相同的,也就是说如果这个数字是有周期的那种,只能算最小周期内的数,比如505050,我们只需要算50就可以

第一次接触扩展kmp,说实话现在连kmp用的都不怎么熟,这个题也是,还没搞懂呢,只是知道了原理,但是字符串操作下标准确性很重要,是否加1等很纠结啊

我觉得这个和一般的扩展kmp有点区别,它这个并没有求extend数组,它这里的next就是extend

关于扩展kmp我觉得百度文库的这个ppt还可以,适合新手看http://wenku.baidu.com/view/fc9d8970f46527d3240ce072.html

#include<stdio.h>
#include<string.h> char s[200010];
int next[100005];
int len; void getnext()
{
int i,j,k,a=0,p=0,L;
next[0]=len;
while(a<len-1&&s[a]==s[a+1])
a++;
next[1]=a;
a=1;
for(k=2;k<len;k++)
{
p=a+next[a]-1;//求最远匹配距离
L=next[k-a];//k-a的匹配长度
if(k+L<=p)//是否小于最远距离
{
next[k]=L;//小于的话就是L
}
else
{
j=p-k+1>0?p-k+1:0;//接着p-k+1或从头匹配
while(k+j<len&&s[k+j]==s[j])//这里就是匹配了,直到匹配失败
j++;
next[k]=j;
a=k;//按道理说应该是和原来的比较一下选最大值的啊,可是加判断时间还变长了
}
}
}
int main()
{
int i,j,k,n,t,cas=1,less,e,m;
scanf("%d",&t);
getchar();
while(t--)
{
less=0;
e=0;
m=0;
		for(i=0;i<len;i++)
{
s[len+i]=s[i];
}
s[len+i]='\0';
getnext();
for(i=1;i<=len;i++)//貌似读错题了,看了好多解题报告都带了这句话,坑爹啊,循环的只算一次吗
{
if(i+next[i]>=len)
{
k=len%i?len:i;
break;
}
}
for(i=0;i<k;i++)
{
if(next[i]>=len)//相等的,感觉就一种情况啊
e++;
else if(next[i]>=0)
{
if(s[i+next[i]]>s[next[i]])//next[i]表示模式串下标,i+next[i]就表示s的下标
m++;
else less++;
}
}
printf("Case %d: %d %d %d\n",cas++,less,e,m);
}
return 0;
}

												

hdu4333 Revolving Digits(扩展kmp)的更多相关文章

  1. HDU 4333 Revolving Digits 扩展KMP

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

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

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

  3. HDU 4333 Revolving Digits 扩张KMP

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

  4. [hdu4333]Revolving Digits

    /*注意注意:本题非hdu4333原题,而是简化版,原版有多组数据.但此代码在修改输入后依旧可以通过多组数据*/ 给出一个数字串,问有多少本质不同同构串比原串小,一样,大.同构串是指,对于原串S[1- ...

  5. 学习系列 - 马拉车&扩展KMP

    Manacher(马拉车)是一种求最长回文串的线性算法,复杂度O(n).网上对其介绍的资料已经挺多了的,请善用搜索引擎. 而扩展KMP说白了就是是求模式串和主串的每一个后缀的最长公共前缀[KMP更像是 ...

  6. 【HDU4333】Revolving Digits(扩展KMP+KMP)

    Revolving Digits   Description One day Silence is interested in revolving the digits of a positive i ...

  7. 字符串(扩展KMP):HDU 4333 Revolving Digits

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

  8. 扩展KMP - HDU 4333 Revolving Digits

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

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

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

随机推荐

  1. sql 循环语句几种方式(变量循环,游标循环,事务)

    --第一   ) ,),Orders )) declare @n int,@rows int --select @rows=count(1) from pe_Orders select @rows = ...

  2. linux服务器如何添加sudo用户

    1. 编辑 vi /etc/ssh/sshd_config 文件,修改默认端口:默认Port为22,并且已经注释掉了,修改是把注释去掉,并修改成其它的端口. 原来用默认端口:22修改为:8975 (这 ...

  3. webRTC视频通话,https协议,录制端和播放端

    最近做视频直播模块,在网上也看到很多大神写的代码,写的都不错,但不是我想要的,有的可能比较老,不支持https协议,有的又将直播端和显示端放在一个程序中,不利于我使用,则本篇着重添加了https协议( ...

  4. Intellij Idea 使用时总是打开上次的项目

    Appearance&Behaviour-->>System Settings-->>Startup&Shutdown-->>Reopen last ...

  5. 第四届CCF软件能力认证

    1.图像旋转 问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可. 输入格式 输 ...

  6. docker:一个支持django的dockerfile

    其中,包括了主要的生产环境模块, 从alpine作起,镜像不大.保存用. FROM alpine:3.7 COPY . /target-dir WORKDIR /target-dir RUN sed ...

  7. Kubernetes Master节点灾备恢复操作指南---升级版

    本文档简述了Kubernetes主节点灾备恢复的相关步骤,供在发生k8s master崩溃时操作. 就算是在k8s里部署了etcd群集, 主节点控制组件的高可用节点,灾备恢复也是必须要实现的操作,才能 ...

  8. chunk writer 中需要对抛错的交易进行回滚,同时又要在其他表中记录是哪一笔交易记录失败

    首先根据我有限的知识判断,回滚之后进行写表,该写表动作只能使用listener来进行. 考虑使用的listener有:ItemWriteListener     StepExecutionListen ...

  9. 搭建项目vue + vue-router + vuex + vue-i18n + element-ui + egg + sequelize

    vue + vue-router + vuex + vue-i18n + element-ui + egg + sequelize https://www.cnblogs.com/wuguanglin ...

  10. Vue常用模板语法

    常用模板语法   本篇将在上一篇的基础上记录文本渲染.表达式.过滤器以及常用指令的简单用法. 一.文本渲染 Vue支持动态渲染文本,即在修改属性的同时,实时渲染文本内容.同时为了提高渲染效率,也支持只 ...