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. FPGA学习笔记. 二分频和三分频

    二分频和三分频 二分频:将输入频率CLK分为原来的 1/2 . 实现:在每次CLK的上升沿或下降沿将输出翻转. 三分频: 1/3占空比. 实现:可使用上升沿或下降沿计数生成输出.需要一个两位计数器. ...

  2. ruby http爬虫中的 :body 用法问题

    require 'http' url = 'http://localhost/b.php' data = 'whoami=whoami' html = HTTP.via('127.0.0.1',808 ...

  3. 6 个 Linux 运维典型问题,大牛的分析解决思路在这里 【转】

    作为一名合格的 Linux 运维工程师,一定要有一套清晰.明确的解决故障思路,当问题出现时,才能迅速定位.解决问题,这里给出一个处理问题的一般思路: 重视报错提示信息:每个错误的出现,都是给出错误提示 ...

  4. poj2054

    题意:给定一棵树,每个节点有一个权值,现要求给这些节点进行排列,设排列后的节点顺序为v1~vn,它们的权值是w1~wn,那么我们要求一种排列使得w1*1+w2*2+...+wn*n最小.还有一个限制就 ...

  5. jquery-扩展

    jQuery扩展三种方式:$.extend,$.fn.extend,外部文件. 1)jQuery.extend(object) 调用   $.方法 2)jQuery.fn.extend(object) ...

  6. python的map,filter,reduce学习

    python2,python3中map,filter,reduce区别: 1,在python2 中,map,filter,reduce函数是直接输出结果. 2,在python3中做了些修改,输出前需要 ...

  7. 《Redis设计与实现》学习笔记

    第2章 简单动态字符串(SDS) redis的字符串不是直接用c语言的字符串,而是用了一种称为简单动态字符串(SDS)的抽象类型,并将其作为默认字符串. redis中包含字符串值的键值对在底层都是由S ...

  8. 【AtCoder】ARC085

    C - HSI 题解 \(E = 1900 * (N - M) + 100 * M + \frac{1}{2^{M}} E\) \(E = 2^{M}(1900 * (N - M) + 100 * M ...

  9. Anaconda完全入门指南

    Anaconda完全入门指南 参考文章: 致Python初学者:Anaconda入门使用指南 Anaconda使用总结 概述 很多学习python的初学者甚至学了有一段时间的人接触到anaconda或 ...

  10. Angular 个人深究(一)【Angular中的Typescript 装饰器】

    Angular 个人深究[Angular中的Typescript 装饰器] 最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码. 注:本人前端小白一枚,文章旨在记录自己 ...