HDU  4333

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
 

题意:给一个数,从这个数的尾端取一个数字放在前段,循环进行,输出有多少得到的数大于原来的数、等于原来的数、小于原来的数,注意相同的数只计算一次。‘

思路:使用ex_KMP算法可以得到从第i个字符开始和前缀匹配的最大长度,所以和原来的数进行比较大小时,可以减少比较次数节省时间。由于相同的数只能计算一次,所以可以使用KMP算法找出最小周期,对最小周期的字符进行计算。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int nex[],nex2[];
int a,b,c;
char str[]; void ex_next(int length)
{
///nex[i]: 以第i位置开始的子串与T的前缀的最大长度;
int i;
nex[]=length;
for(i=; i<length-&&str[i]==str[i+]; i++); ///前缀都是同一个字母的时候;
nex[]=i;
int a=;///a为使匹配到最远的地方时的起始匹配地点;
for(int k=; k<length; k++)
{
int p=a+nex[a]-,L=nex[k-a];
if( (k-)+L>=p )
{
int j=(p-k+)>?(p-k+):;
while(k+j<length&&str[k+j]==str[j]) j++;
/// 枚举(p+1,length) 与(p-k+1,length) 区间比较;
nex[k]=j,a=k;
}
else nex[k]=L;
}
} void next_(int len)
{
int k=;
nex2[]=;
for(int i=; i<len; i++)
{
while(k>&&str[k]!=str[i])
k=nex2[k-];
if(str[k]==str[i])
k++;
nex2[i]=k;
}
} void solve(int len)
{
int t,s;
for(int i=; i<len; i++)
{
t=nex[i];
if(i+t<len)
{
if(str[t+i]>str[t]) c++;
else a++;
}
else
{
s=nex[t];
if(s<i)
{
if(str[t+s]<str[s]) c++;
else a++;
}
}
}
} int main()
{
int T,Case=;
scanf("%d",&T);
while(T--)
{
a=;
b=;
c=;
scanf("%s",str);
int len=strlen(str);
next_(len);
int minn=len-nex2[len-];
if(len%minn==) str[minn]='\0';
else minn=len;
//cout<<minn<<endl;
ex_next(minn);
solve(minn);
printf("Case %d: %d %d %d\n",Case++,a,b,c);
}
return ;
}

KMA & ex_KMP---Revolving Digits的更多相关文章

  1. Revolving Digits[EXKMP]

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

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

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

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

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

  4. HDU 4333 Revolving Digits 扩张KMP

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

  5. hdu4333 Revolving Digits(扩展kmp)

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

  6. 扩展KMP - HDU 4333 Revolving Digits

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

  7. HDU - 4333 :Revolving Digits (扩展KMP经典题,问旋转后有多少个不同的数字小于它本身,等于它本身,大于它本身。)

    One day Silence is interested in revolving the digits of a positive integer. In the revolving operat ...

  8. Hdu 4333 Revolving Digits(Exkmp)

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

  9. Revolving Digits(hdu4333)

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

  10. Revolving Digits

    题面 [题目描述]: 有一天,Silence对可以旋转的正整数十分感兴趣.在旋转操作中,他可以把后面的数字按照原位置不动地搬到剩下位置的前面.当然,他也可以完全不动这串数字.比如,他可以把123变为1 ...

随机推荐

  1. 【Cocos2d-Js基础教学(4)cocostudio在cocos2dx-Js中的使用】

    首先我们打开官方网站www.cocos2d-x.org,下载我们安装最新的cocostudio(cocos). 简介: Cocos Studio升级为cocos.更优秀的产品.更优质的服务.游戏开发一 ...

  2. 读写文本(.txt)文件 .NET

    http://www.cnblogs.com/jx270/archive/2013/04/14/3020456.html (一) 读取文件 如果你要读取的文件内容不是很多,可以使用 File.Read ...

  3. 一次流量稍高导致web项目慢的问题&解决

    项目上线后,用户越来越多,有一天出现一个issue:用户访问特别慢. 首先介绍下架构: haproxy/Nginx / \ node1 node2 | | redis redis(slave) | | ...

  4. css中的position:relative和absolute 属性

    语法: position : static | absolute | fixed | relative 取值: static :默认值.无特殊定位,对象遵循HTML定位规则 absolute :将对象 ...

  5. Gradle 脚本剪片---copy

    Gradle是以Groovy语言为基础,基于DSL语法的自动化构建工具,一个构建脚本能够包含任何Groovy语言元素,每个脚本都是UTF-8编码的文件. 6-1 Project对象API 前面我们说过 ...

  6. ASP.NET 如何发现问题的方法

    1.打开IntelliTrace,查看遇到的问题: 2.打开IE8以上,按F12,启动js调试,查看遇到的错误:

  7. Part 1: Running Oracle E-Business Suite on Oracle Cloud

    You can now run Oracle E-Business Suite on Oracle Cloud.  EBS customers can take advantage of rapid ...

  8. Android代码优化工具——Android lint

    作为移动应用开发者,我们总希望发布的apk文件越小越好,不希望资源文件没有用到的图片资源也被打包进apk,不希望应用中使用了高于minSdk的api,也不希望AndroidManifest文件存在异常 ...

  9. Flex Array 与 ArrayCollection.转自网络

    1.array作为控件使用 FLEX3写法: <mx:Array id="barname"> <mx:String>Flash</mx:String& ...

  10. Simplified CommonJS wrapper 与 AMD 标准定义模块差异

    标准的 AMD 模块定义 一个标准的 AMD 模块看起来是这样子: define(['foo', 'foo/bar'], function (foo, bar) { return { doSometh ...