【HDU4333】Revolving Digits(扩展KMP+KMP)
Revolving DigitsDescription
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
341Sample 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)的更多相关文章
- hdu4333 Revolving Digits(扩展kmp)
Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 4333 Revolving Digits 扩展KMP
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意:给以数字字符串,移动最后若干位到最前边,统计得到的数字有多少比原来大,有多少和原来同样,有多少 ...
- HDU 4333 Revolving Digits [扩展KMP]【学习笔记】
题意:给一个数字,每一次把它的最后一位拿到最前面,一直那样下去,分别求形成的数字小于,等于和大于原来数的个数. SAM乱搞失败 当然要先变SS了 然后考虑每个后缀前长为n个字符,把它跟S比较就行了 如 ...
- HDU - 4333 Revolving Digits(拓展kmp+最小循环节)
1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> ...
- [hdu4333]Revolving Digits
/*注意注意:本题非hdu4333原题,而是简化版,原版有多组数据.但此代码在修改输入后依旧可以通过多组数据*/ 给出一个数字串,问有多少本质不同同构串比原串小,一样,大.同构串是指,对于原串S[1- ...
- 字符串(扩展KMP):HDU 4333 Revolving Digits
Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 扩展KMP - HDU 4333 Revolving Digits
Revolving Digits Problem's Link Mean: 给你一个字符串,你可以将该字符串的任意长度后缀截取下来然后接到最前面,让你统计所有新串中有多少种字典序小于.等于.大于原串. ...
- HDU - 4333 :Revolving Digits (扩展KMP经典题,问旋转后有多少个不同的数字小于它本身,等于它本身,大于它本身。)
One day Silence is interested in revolving the digits of a positive integer. In the revolving operat ...
- HDU 4333 Revolving Digits 扩张KMP
标题来源:HDU 4333 Revolving Digits 意甲冠军:求一个数字环路移动少于不同数量 等同 于的数字 思路:扩展KMP求出S[i..j]等于S[0..j-i]的最长前缀 推断 nex ...
随机推荐
- 安卓数据存储(3):SQLite数据库存储
SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级:使用 SQLit ...
- java.lang.NoClassDefFoundError的原因及解决
[O] 安卓应用在低版本(V2.3.6)系统上运行时报错: java.lang.NoClassDefFoundError 完整错误信息如下: 05-29 13:56:13.687: E/Android ...
- pb popmenu弹出式选单位置的问题
在主界面上使用m_main.m_title.PopMenu(PointX(),PointY()),可以正确定位弹出式选单的位置: 在主界面的控件智商为使用m_main.m_title.PopMenu( ...
- Object-C在Nil上调用方法
在Object-C中,nil对象的作用等同于很多其它语言的NULL指针.不同的地方在于,在nil上调用方法不会导致程序崩溃或抛出异常. 这种技术被用在很多地方,但是对于我们来讲,最主要的就是我们不用在 ...
- iOS开发——TTS文本发音
iOS的文本转发音,从iOS7开始,iOS系统自带这个功能.能够实现中文.英文的发音.而且实现的起来非常方便.就像我看到有的博主说的三行代码搞定. (在iOS7之前(目前已不适配了),比如iOS6实现 ...
- html-----015---HTML ASCII 参考手册
HTML 和 XHTML 用标准的 7 比特 ASCII 代码在网络上传输数据. 7 比特 ASCII 代码可提供 128 个不同的字符值. 7 比特 可显示的 ASCII 代码 <html&g ...
- CSS3中的transform
CSS3 转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸. transform 在2D里主要是4个方法.除了rotate 其他都是接受x y值 translate skew rotate sca ...
- checkbox 选择一个checkbox,其他checkbox也会选择
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- Linux常用(持续更新)
1. scp ./bcec_computernode_check.sh root@10.254.3.1:/tmp 2. # uname -a # cat /proc/version # cat /e ...
- 代码方式删除SVN
public static void delect(File s) { File b[] = null; if (s.exists()) {// 判读是否存在 if (s.isDirectory()) ...