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. ch6 影响 MySQLServer 性能的相关因素

    第6章影响 MySQLServer 性能的相关因素 前言: 大部分人都一致认为一个数据库应用系统(这里的数据库应用系统概指所有使用数据库的系统)的性能瓶颈最容易出现在数据的操作方面,而数据库应用系统的 ...

  2. 编译 curl with ssl

    安装 openssh后,使用 curl 的 ./configure --with-ssl 时,报错“找不到 ssl”.因为 curl在 /usr/local/ssl的安装目录下找动态连接库.而ssl默 ...

  3. Struts2的属性驱动与模型驱动的区别

    1.Struts2的属性驱动. Struts2的属性驱动指的是在action中JSP页面的每一个form中的name都对应在action中有一个属性与之对应.看下面代码片段: <form act ...

  4. 再也不必当心我的密码了,多个SAP 客户端自动输入密码

    再也不必当心我的密码了,多个SAP 客户端自动输入密码问题: 通常对于OFFICE人员来说,一天有很多的密码,OA密码,多个ERP密码,邮箱密码,代理密码等等,还经常60天过期之类,实在是焦头烂额. ...

  5. ViewHolder模式超简洁写法

    ViewHolder是什么就不解释了.大家通常怎么写ViewHolder呢? ViewHolder holder = null; if (convertView == null) { convertV ...

  6. SQL 存储过程入门(事务)(四)

    SQL 存储过程入门(事务)(四)   本篇我们来讲一下事务处理技术. 为什么要使用事务呢,事务有什么用呢,举个例子. 假设我们现在有个业务,当做成功某件事情的时候要向2张表中插入数据,A表,B表,我 ...

  7. HTML5新特性之移动设备API

    为了更好地为移动设备服务,HTML5推出了一系列针对移动设备的API. 1.Geolocation API Geolocation接口用于获取用户的地理位置.它使用的方法基于GPS或者其他机制(比如I ...

  8. LLVM

    http://blog.csdn.net/snsn1984/article/details/41077763

  9. vs.php调试php使用外部的apache进行调试

    vs.php中使用外部的apache进行调试 一般phper们都会有自己配置好的开发调试环境,那么如何在vs.php中使用自己已经配置好的apache+php环境调试 php程序呢?如下: (1)从官 ...

  10. Maximum Entropy Markov Models for Information Extraction and Segmentation

    1.The use of state-observation transition functions rather than the separate transition and observat ...