App 2.0开发模式的行业看法
799
2022-08-26
HDU 4333 Revolving Digits (扩展KMP)
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
1341
Sample Output
Case 1: 1 1 1
题意
给一个数字,每一次把它的最后一位拿到最前面,一直那样下去,分别求形成的数字小于,等于和大于原来数的个数。
例如: 134 可以形成 134,341,413 三个数,所以分别是 1,1,1 。
思路
因为题目给出数的范围非常大 [:10^100000] 所以当然不能用整数处理啦~
采用字符串存储,然后我们可以把当前串复制两次构造出一个新串,利用扩展 KMP 求出 s[i...n-1] 与 s[0...n-1] 的最长公共前缀,然后遍历原串,如果 next[i]>=n 即说明遇到了一个与原串相同的子串,如果 str[next[i]]>str[i+next[i]] 说明找到一个小于原串的子串,否则大于原串。
需要注意的是可能存在相同的子串,如 3131 复制两遍变为 31313131 ,而里面存在相同子串 1313 等。
所以我们要先求出原串的循环节个数,最终根据扩展 KMP 求解后除掉那部分即可。
AC 代码
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。