Revolving Digits

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

Total Submission(s): 25518 Accepted Submission(s): 5587

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

2012 Multi-University Training Contest 4

/*
exkmp.
把原串copy一遍.
先处理出每个后缀与原串的最长公共前缀长度.
Next[i]表示以i为始的最长公共前缀长度.
然后做exkmp只比较每个后缀的前len个字符.
然后比较Next[i]与len的关系.
Next[i]>=len显然匹配前i个字符成功.
t[Next[i]]>s[i+Next[i]]说明该串较小.
(s串为t串copy后的串).
t[Next[i]]<t[i+Next[i]]说明该串较大.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 200001
using namespace std;
char t[MAXN],s[MAXN];
int Next[MAXN],tot,tot1,tot2,tot3;
void kmp()
{
int l=strlen(t);
int i=0,j=-1;
Next[i]=-1;
while(i<l)
{
if(j==-1||t[i]==t[j]) i++,j++,Next[i]=j;
else j=Next[j];
}
}
void get_next()
{
int a=0,l=strlen(t);
Next[0]=l;
while(a<l-1&&t[a]==t[a+1]) a++;
Next[1]=a;a=1;
for(int k=2;k<l;k++)
{
int p=a+Next[a]-1,L=Next[k-a];
if(k+L-1>=p){
int j=max(p-k+1,0);
while(k+j<l&&t[k+j]==t[j]) j++;
Next[k]=j;
a=k;
}
else Next[k]=L;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",t);
int l=strlen(t);
kmp();
int k=l-Next[l],x;
if(l%k==0) x=l/k;
else x=1;
for(int i=0;i<l;i++) t[l+i]=t[i];
get_next();
tot1=tot2=tot3=0;
for(int i=0;i<l;i++)
{
if(Next[i]>=l) tot2++;
else if(t[Next[i]]>t[i+Next[i]]) tot1++;
else if(t[Next[i]]<t[i+Next[i]]) tot3++;
}
tot1/=x,tot2/=x,tot3/=x;
printf("Case %d: %d %d %d\n",++tot,tot1,tot2,tot3);
}
return 0;
}

Hdu 4333 Revolving Digits(Exkmp)的更多相关文章

  1. HDU 4333 Revolving Digits 扩张KMP

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

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

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

  3. 扩展KMP - HDU 4333 Revolving Digits

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

  4. HDU 4333 Revolving Digits 扩展KMP

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意:给以数字字符串,移动最后若干位到最前边,统计得到的数字有多少比原来大,有多少和原来同样,有多少 ...

  5. HDU - 4333 Revolving Digits(扩展KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意 一个数字,依次将第一位放到最后一位,问小于本身的数的个数及等于本身的个数和大于本身的个数,但是要注意 ...

  6. 【扩展kmp+最小循环节】HDU 4333 Revolving Digits

    http://acm.hdu.edu.cn/showproblem.php?pid=4333 [题意] 给定一个数字<=10^100000,每次将该数的第一位放到放到最后一位,求所有组成的不同的 ...

  7. HDU 4333 Revolving Digits

    扩展KMP的应用 我们发现本题的关键在于如何高效的判断两个同构字符串的大小关系,想到如果能够预处理出每一个同构字符串与原字符串的最长公共前缀,那么直接比较它们不一样的部分就好,扩展KMP正好是用来处理 ...

  8. HDU 4333 Revolving Digits [扩展KMP]【学习笔记】

    题意:给一个数字,每一次把它的最后一位拿到最前面,一直那样下去,分别求形成的数字小于,等于和大于原来数的个数. SAM乱搞失败 当然要先变SS了 然后考虑每个后缀前长为n个字符,把它跟S比较就行了 如 ...

  9. hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)

    传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...

随机推荐

  1. Python运算符和编码

    Python运算符和编码 一.格式化输出 现在有以下需求,让⽤户输入name, age, job,hobby 然后输出如下所⽰: ----------info of dogfa---------- n ...

  2. GAN——ModeCollapse

    GAN——ModeCollapse 2017年05月21日 13:54:31 LiuSpark 阅读数 6821更多 分类专栏: 机器学习   版权声明:本文为博主原创文章,遵循CC 4.0 BY-S ...

  3. hdu 6082 2017百度之星资格赛

    #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #inclu ...

  4. hdu 2821 学习一点dfs的小技巧吧。。 还是自己太弱了

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; int r,c ...

  5. IExtenderProvider,c#组件扩展控件属性

    [ProvideProperty("IsEnabled", typeof(LayoutControlItem)), ToolboxItemFilter("System.W ...

  6. Image 对象事件

    以前没怎么注意image上的事件 Image 对象事件 事件 描述 W3C onabort 当用户放弃图像的装载时调用的事件句柄. Yes onerror 在装载图像的过程中发生错误时调用的事件句柄. ...

  7. 一次解决黑帽SEO的经历

    最近有个朋友跟我说他的网站被黑了,百度快照里显示的是另一个网站,如: 于是查找了些资料,终于找到了问题所在,记录如下: 关于黑帽SEO1.暗链:其实“暗链”就是看不见的网站链接,“暗链”在网站中的链接 ...

  8. JavaScript获取数组索引

    JavaScript获取数组索引: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  9. elment-ui表单验证

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-widt ...

  10. 转载:PHP扩展函数库-文件系统、进程与网络

    PHP的扩展函数库十分庞大,官方的非官方的,在这里只记录一些目前比较常用的扩展,对于这一部分,也只是记录其中一些核心的函数,不是一个全面记录.对于详细的扩展函数说明,需要在使用中参考PHP的用户手册. ...