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. photoshop 常用快捷键大全

    一.文件新建 CTRL+N打开 CTRL+O 打开为 ALT+CTRL+O关闭 CTRL+W保存 CTRL+S 另存为 CTRL+SHIFT+S另存为网页格式 CTRL+ALT+S打印设置 CTRL+ ...

  2. ACM——回文

    回文回文! 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:967            测试通过:338 描述 回文是一种有趣的现 ...

  3. ios 阻止GDB依附

    GDB,IDE是大多数hackers的首选,阻止GDB依附到应用的常规办法是: . #import <sys/ptrace.h> . . int main(int argc, charch ...

  4. 控制器的跳转-modal与push

    一.modal与pushmodal从下面往上盖住原来的控制器,一般上一个控制器和下一个控制器没有什么关联时用modal,比如联系人的加号跳转页面,任何控制器都可以用modal push一般是上下文有关 ...

  5. java新手笔记6 示例for

    1.计算天数 /*给定一个年月日,计算是一年的第几天 (如输入:2 15 结果:第46天) */ public class Demo1 { public static void main(String ...

  6. mysql学习笔记2

    drop database 数据库名称;————删除数据库 show columns from 数据表名[from 数据库名]:(或者 show columns from 数据库.数据表名:)———— ...

  7. zlib压缩解压示例

    #include <stdio.h> #include <string.h> #include <assert.h> #include "zlib.h&q ...

  8. Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)

    Problem Description Considera positive integer X,and let S be the sum of all positive integer diviso ...

  9. java中的JSON对象的使用

    申明:没工作之前都没听过JSON,可能是自己太菜了.可能在前台AJAX接触到JSON,这几天要求在纯java的编程中,返回JSON字符串形式. 网上有两种解析JSON对象的jar包:JSON-lib. ...

  10. python 自动化之路 day 07 面向对象基础

    本节内容:   面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法.   面向对象编程 OOP编程是利用"类"和"对象" ...