博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Revolving Digits(hdu4333)
阅读量:5134 次
发布时间:2019-06-13

本文共 4176 字,大约阅读时间需要 13 分钟。

Revolving Digits

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

Total Submission(s): 24215    Accepted Submission(s): 5268

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
题意:将一个数的每一个后缀移到前面和剩下的数字组成新的数字,求这些数字中比原来数字大的,相等的,小的,且不能重复的个数.
思路:将原来的串复制到原窜的后面,那么这样就可以得到,将后缀移到前面的相同的字串,其实就相当于将后缀串移到前面。那后一般的思想是,暴力比较。
假设原串的长度为l,那么新串就为2*l;拿原串与新串[1,l]比较。这样可以用EXKMP来优化,如果在这个点处,extend[i]>=l,那么这个点处往后再数l-1个点时,这个数就和
原数相等,否则小于的话就比较tt[i+extend[i]]-'0'>cc[extend[i]+1]-'0'的大小就好了。最后去重的话,用KMP的next数组,求下循环节,再每个数除循环节就可以了。
还有一种去重复的就是,最后相等的答案肯定是1,所以要除的那个数就是相等的个数;
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 char tt[2*100005]; 12 int extend[2*100005]; 13 int nex[100005]; 14 char d[100005]; 15 char cc[100005]; 16 int pp[100005]; 17 void next1(int k); 18 void EXkmp(int k,int r); 19 using namespace std; 20 int main(void) 21 { 22 int i,j,k,p,q; 23 scanf("%d",&k); 24 for(int s =1; s<=k; s++) 25 { 26 memset(nex,0,sizeof(nex)); 27 memset(extend,0,sizeof(extend)); 28 scanf("%s",d); 29 int l=strlen(d); 30 for(i=1; i<=2*l; i++) 31 { 32 if(i<=l) 33 tt[i]=d[i-1]; 34 else 35 tt[i]=d[i-l-1]; 36 } 37 for(i=0; i
0&&cc[j+1]!=cc[i]) 45 { 46 j=pp[j]; 47 } 48 if(cc[j+1]==cc[i]) 49 { 50 j++; 51 } 52 pp[i]=j; 53 } 54 int temp=l/(l-pp[l]); 55 if(l%(l-pp[l])!=0) 56 { 57 temp=1; 58 } 59 next1(l); 60 EXkmp(2*l,l); 61 int a[4]; 62 memset(a,0,sizeof(a)); 63 for(i=1; i<=l+1; i++) 64 { 65 if(extend[i]==l)//当匹配数等于l时就相等 66 { 67 a[1]++; 68 } 69 else 70 { 71 if(tt[i+extend[i]]-'0'>cc[extend[i]+1]-'0')//不等于l时比较开始不相等的那位 72 { 73 a[0]++; 74 } 75 else a[2]++; 76 } 77 } 78 printf("Case %d: ",s); 79 printf("%d %d %d\n",a[2]/temp,(a[1]-1)/temp,a[0]/temp); 80 81 } 82 83 } 84 void next1(int k) 85 { 86 int i,j,p; 87 j=1; 88 int r=j; 89 nex[1]=0; 90 while(cc[j+1]==cc[j]&&j+1<=k) 91 { 92 j++; 93 } 94 nex[2]=j-r; 95 int id=2; 96 for(i=3; i<=k; i++) 97 { 98 p=id+nex[id]-1; 99 int L=nex[i-id+1];100 int c=i+L-1;101 if(c>=p)102 {103 int j=p-i+1;104 if(j<0)j=0;105 while(cc[j+1]==cc[j+i]&&j+i<=k)106 {107 j++;108 }109 nex[i]=j;110 id=i;111 }112 else nex[i]=L;113 }114 }115 116 void EXkmp(int k,int r)117 {118 int i,j;119 j=0;120 while(cc[j+1]==tt[j+1]&&j+1<=r)121 {122 j++;123 }124 extend[1]=j;125 int id=1;126 int p;127 for(i=2; i<=k; i++)128 {129 p=id+extend[id]-1;130 int L=nex[i-id+1];131 int c=i+L-1;132 if(c>=p)133 {134 j=p-i+1;135 j=max(j,0);136 while(cc[j+1]==tt[j+i]&&j+i<=k&&j<=r)137 {138 j++;139 }140 extend[i]=j;141 id=i;142 }143 else extend[i]=L;144 }145 }

 

 

转载于:https://www.cnblogs.com/zzuli2sjy/p/5186473.html

你可能感兴趣的文章
今天新开通了博客
查看>>
AS3优化性能笔记二
查看>>
ElasticSearch(站内搜索)
查看>>
4----COM:a Generative Model for group recommendation(组推荐的一种生成模型)
查看>>
UVA 11137 - Ingenuous Cubrency
查看>>
js阻止事件冒泡的两种方法
查看>>
Java异常抛出
查看>>
[SQL Server 系] T-SQL数据库的创建与修改
查看>>
74HC164应用
查看>>
变量声明和定义的关系
查看>>
Wpf 之Canvas介绍
查看>>
linux history
查看>>
jQuery on(),live(),trigger()
查看>>
Python2.7 urlparse
查看>>
sencha touch在华为emotion ui 2.0自带浏览器中圆角溢出的bug
查看>>
【架构】Linux的架构(architecture)
查看>>
ASM 图解
查看>>
Date Picker控件:
查看>>
你的第一个Django程序
查看>>
grafana授权公司内部邮箱登录 ldap配置
查看>>