Revolving Digits
 

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
 

【题意】

  给定一个数字<=10^100000,一次将该数的第一位放到放到最后一位,求所有组成的不同的数比原数小的个数,相等的个数,大的个数。

【分析】

  扩展KMP和KMP,KMP用来求循环节。

扩展KMP部分的代码(求next):

void exkmp()
{
nt[1]=2*l;int mx=0,id;
while(mx+1<=2*l&&s[mx+1]==s[mx+2]) mx++;
nt[2]=mx;id=2;
for(int i=3;i<=2*l;i++)
{
mx=id+nt[id]-1;
int now=nt[i-id+1];
if(i+now<=mx) nt[i]=now;
else
{
int j=mx-i+1;
if(j<0) j=0;
while(i+j<=2*l&&s[i+j]==s[1+j]) j++;
nt[i]=j;id=i;
}
}
}

  

KMP部分代码(求next):

(注意红色部分)

啦啦啦啦

本题代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 100010 char s[*Maxn];
int l; int nt[*Maxn];
void exkmp()
{
nt[]=*l;int mx=,id;
while(mx+<=*l&&s[mx+]==s[mx+]) mx++;
nt[]=mx;id=;
for(int i=;i<=*l;i++)
{
mx=id+nt[id]-;
int now=nt[i-id+];
if(i+now<=mx) nt[i]=now;
else
{
int j=mx-i+;
if(j<) j=;
while(i+j<=*l&&s[i+j]==s[+j]) j++;
nt[i]=j;id=i;
}
}
} int ntt[Maxn*]; void kmp()
{
ntt[]=;int p=;
for(int i=;i<=l;i++)
{
while((s[i]!=s[p+]&&p)||p>=l) p=ntt[p];
if(s[i]==s[p+]&&p+<=l) p++;
ntt[i]=p;
}
} int main()
{
int T,kase=;
scanf("%d",&T);
while(T--)
{
scanf("%s",s+);
l=strlen(s+);
for(int i=;i<=l;i++) s[i+l]=s[i];
exkmp();
int a1=,a2=,a3=;
for(int i=;i<=l;i++)
{
if(nt[i]>=l) a2++;
else if(s[i+nt[i]]<s[+nt[i]]) a1++;
else a3++;
}
int x=;
kmp();
if(l%(l-ntt[l])==) x=l/(l-ntt[l]);
printf("Case %d: ",++kase);
printf("%d %d %d\n",a1/x,a2/x,a3/x);
}
return ;
}

[HDU4333]

2016-08-19 14:44:52

												

【HDU4333】Revolving Digits(扩展KMP+KMP)的更多相关文章

  1. hdu4333 Revolving Digits(扩展kmp)

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

  2. HDU 4333 Revolving Digits 扩展KMP

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

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

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

  4. HDU - 4333 Revolving Digits(拓展kmp+最小循环节)

    1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> ...

  5. [hdu4333]Revolving Digits

    /*注意注意:本题非hdu4333原题,而是简化版,原版有多组数据.但此代码在修改输入后依旧可以通过多组数据*/ 给出一个数字串,问有多少本质不同同构串比原串小,一样,大.同构串是指,对于原串S[1- ...

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

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

  7. 扩展KMP - HDU 4333 Revolving Digits

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

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

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

  9. HDU 4333 Revolving Digits 扩张KMP

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

随机推荐

  1. C# ADO.NET参数查询

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. 20151214 jquery插件代码备份

    ;(function ($) { //局部性的 /*$.fn.extend({ 'nav' : function (color) { $(this).find('.nav').css({ 'list- ...

  3. mysql更改root密码及root远程登录

    1.更改root密码 use mysql; update user set password=password('petecc') where user='root'; 2.root远程登录 1 up ...

  4. 最近在学习UDP方面的通信,找到一个很棒的博客

    http://blog.csdn.net/kesalin/article/details/8798039

  5. iOS-scrollview及其子类适配iOS7

    问题描述: 在iOS7之后如果在导航控制器中所属的字控制器中嵌入scrollview及其子类的视图,当scrollview的尺寸太小的话不会调用返回cell的方法.控制器的嵌套层级结构如下图所示,着重 ...

  6. struts2框架加载配置文件的顺序

    struts-default.xml:该文件保存在struts2-core-x.x.x.jar文件中: struts-plugin.xml:该文件保存在 struts2-Xxx-x.x.x.jar等S ...

  7. WPF简单拖拽功能实现

    1.拖放操作有两个方面:源和目标. 2.拖放操作通过以下三个步骤进行: ①用户单击元素,并保持鼠标键为按下状态,启动拖放操作. ②用户将鼠标移到其它元素上.如果该元素可接受正在拖动的内容的类型,鼠标指 ...

  8. avconv转换视频

    提取指定stream time avconv -i i.mkv -map 0:0 -map 0:1 -map 0:5 -c:v copy -c:a:0 mp3 -c:s copy o.mkv 合并 a ...

  9. (转)C++静态库与动态库

    转自:http://www.cnblogs.com/skynet/p/3372855.html C++静态库与动态库 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别, ...

  10. IOS 学习笔记 2015-03-24 OC-API-网络访问-案例一

    // // WPSuggest.h // OC-API-网络访问 // // Created by wangtouwang on 15/3/24. // Copyright (c) 2015年 wan ...