您的位置:首页 > 其它

HDU 4333 Revolving Digits (扩展KMP)

2012-11-09 20:43 471 查看

Revolving Digits

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

1 341

[align=left]Sample Output[/align]

Case 1: 1 1 1

[align=left]Source[/align]
2012 Multi-University Training Contest 4

[align=left]Recommend[/align]
zhoujiaqi2010

扩展KMP能求出一个串所有后缀串(即s[i...len])和模式串的最长公共前缀。于是只要将这个串复制一遍,求出该串每个后缀与其本身的最长公共前缀即可,当公共前缀>=len时,显然相等,否则只要比较下一位就能确定这个串与原串的大小关系。
  至于重复串的问题,只有当这个串有循环节的时候才会产生重复串,用KMP的next数组求出最小循环节,用长度除以最小循环节得到循环节个数,在将3个答案都除以循环节个数即可。

//============================================================================
// Name        : HDU4333.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
const int MAXN=2000010;

int next[MAXN];
int extend[MAXN];

void EKMP(char s[],char t[])//s为主串,t为模板串
{
int i,j,p,L;
int lens=strlen(s);
int lent=strlen(t);
next[0]=lent;
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 && j<lent && 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 getNext(char T[],int len)
{
int j,k;
j=0;k=-1;next[0]=-1;
while(j<len)
{
if(k==-1 || T[j]==T[k])
next[++j]=++k;
else k=next[k];
}
}
char str1[MAXN],str2[MAXN];
int main()
{
int T;
int iCase=0;
scanf("%d",&T);
while(T--)
{
iCase++;
scanf("%s",str1);
int len=strlen(str1);
strcpy(str2,str1);
strcat(str2,str1);
EKMP(str2,str1);
int cnt1=0,cnt2=0,cnt3=0;

for(int i=0;i<len;i++)
{
if(extend[i]>=len)cnt2++;
else
{
if(str2[i+extend[i]]<str1[extend[i]])cnt1++;
else cnt3++;
}
}
getNext(str1,len);
int t=len-next[len];
int tol=1;
if(len%t==0)tol=len/t;
printf("Case %d: %d %d %d\n",iCase,cnt1/tol,cnt2/tol,cnt3/tol);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: