您的位置:首页 > 其它

Revolving Digits HDU - 4333 (扩展kmp)

2018-02-05 15:30 483 查看

Revolving Digits

HDU - 4333

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


因为这个题,能用的数字都是固定的,而且按顺序循环变换位置,所以将原字符串变成二倍,然后用扩展kmp即可

如果长度和原长度相同就是相等,其他情况只需要比较一下彼此第一个不相等的数字看看大小就知道谁大谁小了。

如果有字符串循环的情况说明计算的个数有重的,最后要除以它的循环周期

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 2000010;
char t[MAXN],s[MAXN];
int Next[MAXN],Next2[MAXN];
int extend[MAXN];
void getNext(){
int i = -1,j = 0;
int len = strlen(s);
Next2[0] = -1;
while(j < len){
if(i == -1 || s[i] == s[j]){
i++,j++;
Next2[j] = i;
}
else
i = Next2[i];
}
}
void e_kmp(){
int i,j,a,p,L;
int lent = strlen(t);
int lens = strlen(s);
j = 0;
Next[0] = lens;
while(j+1 < lens && s[j+1] == s[j])
j++;
Next[1] = j;
a = 1;
for(i = 2; i < lens; i++){
p = Next[a]+a-1;
L = Next[i-a];
if(L+i < p+1)
Next[i] = L;
else{
j = max(0,p-i+1);
while(i+j < lens && s[i+j] == s[j])
j++;
Next[i] = j;
a = i;
}
}
j = 0;
while(j < lens && j < lent && t[j] == s[j])
j++;
extend[0] = j;
a = 0;
for(i = 1; i < lent; i++){
p = extend[a]+a-1;
L = Next[i-a];
if(L+i < p+1)
extend[i] = L;
else{
j = max(0,p-i+1);
while(i+j < lent && j < lens && s[j] == t[i+j])
j++;
extend[i] = j;
a = i;
}
}
}
int main(){
int T;
int cas = 0;

4000
scanf("%d",&T);
while(T--){
scanf("%s",s);
strcpy(t,s);
strcat(t,s);
getNext();
e_kmp();
int lens = strlen(s);
int i,ans1 = 0,ans2 = 0,ans3 = 0;
for(i = 0; i < lens; i++){
if(extend[i] >= lens)
ans2++;
else{
if(t[i+extend[i]] < s[extend[i]])
ans1++;
else
ans3++;
}
}
int tmp = lens-Next2[lens];
int ret = 1;
if(lens % tmp == 0){
ret = lens/tmp;
}
printf("Case %d: %d %d %d\n",++cas,ans1/ret,ans2/ret,ans3/ret);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: