HDOJ 4333 Revolving Digits 扩展KMP

来源:互联网 发布:淘宝 做工粗糙 有瑕疵 编辑:程序博客网 时间:2024/06/05 07:39

Revolving Digits

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23856    Accepted Submission(s): 5145


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
 

Source
2012 Multi-University Training Contest 4
 

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:  4332 4335 4334 4336 4337 


#include <cstdio>#include <cstring>const int N = 100010;char stra[N * 2], strb[N * 2];int next[N * 2];void ex_kmp(const char *S, const char *T) {int t = 0, p = 0;while (S[t] && T[t] && S[t] == T[t])t++;next[0] = t;for (int i = t = 1; S[i]; ++i) {int L = next[i - t];if (i + L > p) {int j = p - i + 1 > 0 ? p - i + 1 : 0;while (S[i + j] && T[j] && S[i + j] == T[j])++j;next[i] = j;t = i;p = i + j - 1;}elsenext[i] = L;}}int main() {int T;scanf("%d", &T);while (T--) {scanf("%s", strb);strcpy(stra, strb);strcat(stra, strb);ex_kmp(strb, strb);ex_kmp(stra, strb);int g = 0, e = 0, l = 0;int len = strlen(strb);for (int i = 0; i < len; ++i) {if (next[i] < len) {if (stra[i + next[i]] > strb[next[i]])g++;elsel++;}elsee++;}static int cnt = 0;printf("Case %d: %d 1 %d\n", ++cnt, l / e, g / e);}return 0;}


0 0
原创粉丝点击