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. Hybris电商方案介绍(企业全渠道) B2B B2C O2O建设

    1). 什么是Hybris: hybris software成立于1997年,2013年与SAP整合,成为SAP旗下的一份子,提供全渠道客户互动与商务解决方案,该解决方案能够为各机构提供客户的实时背景 ...

  2. asp.net中使用ueditor

    原文地址:http://blog.uoolo.com/Article/16 还有在MVC中使用ueditor:http://blog.uoolo.com/Article/111 最初百度了一下“编辑器 ...

  3. POJ 3519 Minimal Backgammon

    Minimal Backgammon Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1195   Accepted: 700 ...

  4. sizeof and strlen整理

    sizeof 定义 计算对象或类型所占用的字节数(byte) 记住是字节数,而不是个数 语法 sizeof(对象) int i; sizeof(i); sizeof(类型) sizeof(int); ...

  5. Android Touch消息传递机制探究分析

    在Android中,消息的传递控制主要是通过两个方法共同配合使用来对用户的触摸消息进行分发的,下面就来看看这两个方法: onInterceptTouchEvent:此方法定义于ViewGroup中,顾 ...

  6. Android Studio NDK 学习之接受Java传入的Int数组

    本博客是基于Android Studio 1.3 preview版本,且默认你已经安装了Android SDK, Android NDK. 用Android Studio新建一个工程叫AndroidJ ...

  7. JavaScript备忘录(3)——正则表达式

    正则表达式是用来进行字符串匹配的. 定义正则表达式有两种方法:/wor/或者new RegExp("wor"). 使用方法 在JS中,使用正则表达式的方法有: 字符串的search ...

  8. STM32 程序所占用空间计算 && FLASH存储的起始地址计算

    程序编译完成,会乘车program size .. 对STM32容量选型或者 计算FLASH 充当EEPROM起始地址时会用到此参数. 按照下面截图  程序空间 = (16700+732+4580)/ ...

  9. NHibernate 使用CreateSQLQuery进行查询

    涉及的表:Cake{Id ,CakeName } CakeSize{ CakeId,-为外键,对应Cake表的字段Id Size } (其中ISession session = NHibernateH ...

  10. 说说你所熟知的MSSQL中的substring函数

    说说你所熟知的MSSQL中的substring函数 *:first-child { margin-top: 0 !important; } body>*:last-child { margin- ...