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. volatile的使用原则

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/4352802.html ...

  2. mysql insert语句错误问题解决

    好久没有复习数据库了,竟然忘记了mysql中的关键字(保留字),导致今天一晚上都在查找sql语句错误,特此记录此错误,教训啊. 我在mysql数据库中有一个名为order 的表,啊啊啊啊啊,为啥我给他 ...

  3. c# winform实现网页上用户自动登陆,模拟网站登录

    using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...

  4. WPF窗体视图中绑定Resources文件中字符串时,抛出:System.Windows.Markup.StaticExtension

    问题描述: 在Resources.resx定义了一个静态字符串字段Title,并在WPF窗体视图中绑定为窗体的标题: Title="{x:Static local:Resources.Tit ...

  5. 学习笔记_过滤器详细_2(过滤器JavaWeb三大组件之一)

    过滤器详细 5 四种拦截方式 我们来做个测试,写一个过滤器,指定过滤的资源为b.jsp,然后我们在浏览器中直接访问b.jsp,你会发现过滤器执行了! 但是,当我们在a.jsp中request.getR ...

  6. Spring 和 MyBatis 环境整合

    本案例主要是讲述Spring  和  MyBatis 的环境整合 , 对页面功能的实现并没有做的很完整 先附上本案例的结构 1 . 创建项目并导入相关jar包 commons-collections4 ...

  7. 常用JS验证和函数

    下面是我常用一些JS验证和函数,有一些验证我直接写到了对象的属性里面了,可以直接通过对象.方法来调用 //浮点数除法运算 function fdiv(a, b, n) { if (n == undef ...

  8. oc ios 中文字符串 进行 sha1加密 错误?

    我在网上找到了一个oc版加密的工具类,但是加密中文就出现大问题 const char *cstr = [self cStringUsingEncoding:encoding]; NSData *dat ...

  9. js自执行函数表达式

    // 下面2个括弧()都会立即执行 (function () { /* code */ } ()); // 推荐使用这个(function () { /* code */ })(); // 但是这个也 ...

  10. jQuery如何阻止子元素继承父元素事件?

    <a> <b></b> </a> $("a").click(...); 这种绑定的话,b也会响应一次事件,如何只对a元素绑定事件,而 ...