hdu4333----Revolving Digits

Revolving Digits

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


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
 

Source
2012 Multi-University Training Contest 4
 

Recommend

学习了下扩展kmp,刘雅琼的ppt讲的很清晰很有条理    点击打开链接

本题的主串就是把模式串变成2倍,然后进行扩展kmp匹配,对于每一个extend[i] 如果 大于等于len,说明相等,不然就比较一下下一位就可以知道大小
存在重复串的问题,只有当串存在最小循环节时,才可能存在重复串,所以可以通过kmp的next来求出最小循环节,然后每个答案都除上这个值

/*************************************************************************
    > File Name: hdu4333.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年01月23日 星期五 14时26分40秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 2000010;

char T[N], S[N];
int next[N];
int next2[N];
int extend[N];
void EXTEND_KMP ()
{
	int lens = strlen (S);
	int lent = strlen (T);
	next[0] = lent;
	int i, j, p, L;
	j = 0;
	while (j + 1 < lent && T[j] == T[j + 1])
	{
		++j;
	}
	next[1] = j;
	int a = 1;
	for (i = 2; i < lent; ++i)
	{
		p = next[a] + a - 1;
		L = next[i - a];
		if (i + L < p + 1)
		{
			next[i] = L;
		}
		else
		{
			j = max (0, p - i + 1);
			while (i + j < lent && T[i + j] == T[j])
			{
				++j;
			}
			next[i] = j;
			a = i;
		}
	}
	j = 0;
	while (j < lens && S[j] == T[j])
	{
		++j;
	}
	extend[0] = j;
	a = 0;
	for (i = 1; i < lens; ++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 < lens && j < lent && S[i + j] == T[j])
			{
				++j;
			}
			extend[i] = j;
			a = i;
		}
	}
}

void get_next()
{
	int len = strlen (T);
	next2[0] = -1;
	int j = 0;
	int k = -1;
	while (j < len)
	{
		if (k == -1 || T[k] == T[j])
		{
			++k;
			++j;
			next2[j] = k;
		}
		else
		{
			k = next2[k];
		}
	}
}

int main ()
{
	int t;
	scanf("%d", &t);
	int icase = 1;
	while (t--)
	{
		scanf("%s", T);
		strcpy (S, T);
		strcat (S, T);
		get_next ();
		EXTEND_KMP ();
		int lens = strlen (S);
		int lent = strlen (T);
		int ans1 = 0, ans2 = 0, ans3 = 0;
		for (int i = 0; i < lent; ++i)
		{
			if (extend[i] >= lent)
			{
				++ans2;
			}
			else
			{
				if (S[i + extend[i]] < T[extend[i]])
				{
					++ans1;
				}
				else
				{
					++ans3;
				}
			}
		}
		int ret = 1;
//		printf("%d %d %d\n", ans1, ans2, ans3);
		if (lent % (lent - next2[lent]) == 0)
		{
			ret = lent / (lent - next2[lent]);
		}
		printf ("Case %d: %d %d %d\n", icase++, ans1 / ret, ans2 / ret, ans3 / ret);
	}
	return 0;
}


你可能感兴趣的:(扩展kmp)