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. nagios使用问题的解决方案

    通过web界面修改某个服务时报错例如对某个服务进行临时安排其执行时间,或者不让它发警告,web页面上都有这样的设置.但是常常会有错误信息如下: Could not open command file ...

  2. 2012 Dhaka

    2012 Dhaka B - Wedding of Sultan 题目描述:给出一棵树的\(dfs\)序(只要经过就会记录),求每个点的度 solution 按\(dfs\)序的规则还原这棵树就好了. ...

  3. urbuntu12.04 ftp服务器搭建

    1.安装ftp服务器: sudo apt-get install vsftpd 2..配置ftp 修改ftp的配置文件,该文件在/etc目录下,在终端中键入如下命令以打开配置文件: sudo vi / ...

  4. js中的事件委托或是事件代理详解(转载)

    起因: 1.这是前端面试的经典题型,要去找工作的小伙伴看看还是有帮助的: 2.其实我一直都没弄明白,写这个一是为了备忘,二是给其他的知其然不知其所以然的小伙伴们以参考: 概述: 那什么叫事件委托呢?它 ...

  5. 利用pt-table-checksum校验数据一致性

    相信很多人的线上都搭建了MySQL主从这样的框架,很多人只监控MySQL的从服务器Slave_IO和Slave_SQL这两个线程是否为YES,还有 Seconds_Behind_Master延迟大不大 ...

  6. nvidia-smi 命令解读

    nvidia-smi 的定义: 基于 NVIDIA Management Library (NVIDIA 管理库),实现 NVIDIA GPU 设备的管理和监控功能 主要支持 Tesla, GRID, ...

  7. Web Sevice平台

    web Service 三种基本元素: SOAP .WSDL .UDDI 什么是SOAP:       XML+HTTP 基本的Web Service平台 SOAP 简易对象访问协议 ,是一宗用于发送 ...

  8. Codeforces Round #480 (Div. 2) E - The Number Games

    题目大意:给你n个点的一棵树, 每个点的权值为2^i ,让你删掉k个点使得剩下的权值和最大. 思路:这题还是比较好想的, 我们反过来考虑, 剩下一个的情况肯定是选第n个点,剩下两个 我们肯定优先考虑第 ...

  9. 在Win10下搭建web服务器,使用本机IP不能访问,但是使用localhos或127.0.0.1可以正常访问的解决办法

    最近在在Win10下搭建web服务器,发现通过windows自带的浏览器win10 edge浏览器使用本机IP不能放问,但是使用localhos或127.0.0.1可以正常访问, 后来无意发现,使用w ...

  10. 001.Parted工具使用

    一 Parted简介 1.1 parted和fdisk 通常使用较多的磁盘管理工具为fdisk,但由于磁盘越来越廉价,且磁盘空间越来越大,而fdisk工具分区存在大小限制,只能划分小于2T的磁盘.因此 ...