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. 剑指offer18:操作给定的二叉树,将其变换为源二叉树的镜像。

    1 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 2 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ ...

  2. BridgeOverARoughRiver(POJ-3404)【AdHoc】

    题目链接:https://vjudge.net/problem/POJ-3404 题意:n个极限速度不同的人要过桥,一开始所有人在桥的一边,每次最多两个人同时过桥,过桥时需要用一把火炬并且全场只有一把 ...

  3. Codeforces 718A Efim and Strange Grade 程序分析

    Codeforces 718A Efim and Strange Grade 程序分析 jerry的程序 using namespace std; typedef long long ll; stri ...

  4. nginx访问量统计 日常分析

    nginx访问量统计 0.查询某个时间段的日志 cat appapi.dayutang.cn.access.log |grep 'POST'|grep '2019:10' > 20191059. ...

  5. 利用Python进行数据分析_Numpy_基础_1

    ndarray:多维数组 ndarray 每个数组元素必须是相同类型,每个数组都有shape和dtype对象. shape 表示数组大小 dtype 表示数组数据类型 array 如何创建一个数组? ...

  6. Python中的幽灵—编码方式

    首先要搞懂本地操作系统编码与系统编码的区别: 本地操作系统编码方式与操作系统有关,Linux默认编码方式为utf-8,Windows默认编码方式为gbk: 系统编码方式与编译器or解释器有关,Pyth ...

  7. xshell和xftp过期解决办法

    去官网 xshell:https://www.netsarang.com/download/down_form.html?code=522 xftp:https://www.netsarang.com ...

  8. Scala学习二十一——隐式转换和隐式参数

    一.本章要点 隐式转换用于类型之间的转换 必须引入隐式转换,并确保它们可以以单个标识符的形式出现在当前作用域 隐式参数列表会要求指定类型的对象.它们可以从当前作用域中以单个标识符定义的隐式对象的获取, ...

  9. Java建造者模式(思维导图)

    图1 建造者模式[点击查看大图] 基本的知识点已在思维导图中,下面是demo 1,Builder 为创建一个产品对象的各个部件指定抽象接口 public interface PersonBuilder ...

  10. python 使用三种常用的工具包处理图片

    matplotlib,PIL(Pillow),Opencv三种常用的作图方式. 使用matplotlib画图,很棒,matplotlib 是python最著名的2D绘图库,它提供了一整套和matlab ...