HDU 4333 Revolving Digits(扩展KMP啊)

来源:互联网 发布:中信淘宝信用卡额度 编辑:程序博客网 时间:2024/04/29 20:22

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4333


不知道扩展KMP适用情况的请猛戳:

ACdreamer:http://blog.csdn.net/acdreamers/article/details/8313828


山路水桥:http://www.cnblogs.com/10jschen/archive/2012/09/03/2668149.html


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


转自:ACdreamer

题意:

给一个数字,每一次把它的最后一位拿到最前面,一直那样下去,分别求形成的数字小于,等于和大于原来数的个数。

例如:134可以形成134,341,413三个数,所以分别是1,1,1。

分析:

由于长度为len的字符串形成题目要求的串的个数为len,那么我们可以把原来的两个串T连接起来形成字符串S,然后找S的每

个后缀的前len个元素即可。

 

这里主要是如何比较的问题,对于字符串的比较,我们可以先求出他们的最长公共前缀长度,然后只需要比较一次就可以知道结果了。那么最长公共前缀怎么求,由于这里是一个串T与另一个串S,来求S的所有后缀与T的最长公共前缀长度,所以用扩展

KMP。如果extend[i]>=len,就说明与原来的相等了,否则如果S[i+extend[i]]<T[extend[i]]就说明小于,否则就是大

于。


代码如下:(kuangbin模板)

#include <stdio.h>#include <math.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int MAXN = 2000010;char str1[MAXN], str2[MAXN];int Next[MAXN];int extend[MAXN];/** 扩展KMP算法*///Next[i]:x[i...m-1]与x[0...m-1]的最长公共前缀//extend[i]:y[i...n-1]与x[0...m-1]的最长公共前缀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];    }}void pre_EKMP(char x[],int m){    Next[0]=m;    int j=0;    while(j+1<m && x[j]==x[j+1])j++;    Next[1]=j;    int k=1;    for(int i=2; i<m; i++)    {        int p=Next[k]+k-1;        int L=Next[i-k];        if(i+L<p+1)Next[i]=L;        else        {            j=max(0,p-i+1);            while(i+j<m && x[i+j]==x[j])j++;            Next[i]=j;            k=i;        }    }}void EKMP(char x[],int m,char y[],int n){//x是模式串, y是主串    pre_EKMP(x,m);    int j=0;    while(j<n && j<m && x[j]==y[j])j++;    extend[0]=j;    int k=0;    for(int i=1; i<n; i++)    {        int p=extend[k]+k-1;        int L=Next[i-k];        if(i+L<p+1)extend[i]=L;        else        {            j=max(0,p-i+1);            while(i+j<n && j<m && y[i+j]==x[j])j++;            extend[i]=j;            k=i;        }    }}int main(){    int t;    int cas = 0;    scanf("%d",&t);    while(t--)    {        scanf("%s",str1);        strcpy(str2,str1);        strcat(str2,str1);        int len1 = strlen(str1);        int len2 = strlen(str2);        EKMP(str1,len1,str2,len2);//str1模式串,str2主串        int cnts = 0,cnte = 0, cntb = 0;        for(int i = 0; i < len1; i++)        {            if(extend[i] >= len1)                cnte++;            else            {                if(str2[i+extend[i]] < str1[extend[i]])                {                    cnts++;                }                else                {                    cntb++;                }            }        }        getNext(str1,len1);        int t = len1-Next[len1];//最小循环节        //printf("len1:%d Next[len1]:%d t:%d\n",len1,Next[len1],t);        int tlen = 1;        if(len1%t == 0)            tlen = len1/t;        cnts/=tlen;        cnte/=tlen;        cntb/=tlen;        printf("Case %d: %d %d %d\n",++cas,cnts,cnte,cntb);    }    return 0;}/*93341123*/


0 0
原创粉丝点击