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. JQuery插件autocomplete使用说明文档

    项目中有时会用到ajax自动补全查询,就像Google的搜索框中那样,输入汉字或者字母的首个字母,则包含这个汉字或者字母的相关条目会显示出来供用户选择,该插件就是实现这样的功能的.autocomple ...

  2. Quartus II 破解教程—FPGA入门教程【钛白Logic】

    这一节主要说明如何破解Quartus II 13.1.首先找到我们提供的破解工具,这里我们的电脑是64位的,所以使用64位破解器.如下图. 第一步:将破解工具拷贝到安装目录下“D:\altera\13 ...

  3. 百度url解析Joe.Smith整理大全

    百度url解析Joe.Smith整理大全 百度url解析Joe.Smith整理大全...1 本文链接:http://blog.csdn.net/qq_26816591/article/details/ ...

  4. 使用配置文件启动MongoDB

    Ubuntu 16.04 (阿里云ECS),MongoDB 4.0, 原来,已经写了10篇MongoDB的随笔了.可是,自己居然没有使用配置文件启动过MongoDB,对其更多的配置是不明白的. 昨天( ...

  5. java虚拟机规范(se8)——java虚拟机结构(二)

    2.5 运行时数据区域 java虚拟机定义了多个用于程序执行期间的运行时数据区域.这些数据区域中一些随着java虚拟机的启动而创建,随着虚拟机的退出而销毁.其他的数据区域时和线程相关的.线程相关数据区 ...

  6. 面试经典---数据库索引B+、B-树

    大型数据库数据都是存在硬盘中的,为了操作的速度,需要设计针对外存的数据结构.而数据库索引技术就是在面试中反复被问到的一个问题:数据库索引是怎么实现的?数据库索引越大越好吗? 需要详细了解下这方面的知识 ...

  7. laravel 5.1 Model 属性详解

    <?php namespace Illuminate\Database\Eloquent; /** * 下面提到某些词的含义: * 1.覆盖: 在继承该类 \Illuminate\Databas ...

  8. 【Java】 归并排序的非递归实现

    归并排序可以采用递归方法(见:归并排序),但递归方法会消耗深度位O(longn)的栈空间,使用归并排序时,应该尽量使用非递归方法.本文实现了java版的非递归归并排序. 更多:数据结构与算法合集 思路 ...

  9. 升级到AndroidStudio3.2.1的注意事项

    升级到AndroidStudio3.2.1的注意事项     即不用再在二级结构的build.gradle中的compileSdkVersion 28 下声明 buildToolsVersion &q ...

  10. 001.NTP简介

    一 NTP简介 ntp服务器顾名思义就是时间同步服务器(Network Time Protocol),时间同步对于计划备份.入侵检测记录.分布式任务调度或者事务订单管理来说都是非常有必要的日常任务. ...