hdu 4333 Revolving Digits ( extend kmp )

来源:互联网 发布:淘宝卖视频教程类目 编辑:程序博客网 时间:2024/05/21 22:47

Revolving Digits

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


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
题目大意:就是求取当前串旋转变化后,能产生的新数字中大于和小于当前数字的个数,相等的一定是1,感觉略水
题目分析:用拓展kmp求取next数组,然后利用next数组的性质求取最小循环节,为了去重,因为只要不出现循环,就不会出现数的重复
然后利用extend数组的性质,找到最靠前的不一样的字符位置,判断该字符大小关系,即能得到整个串的字典序大小关系,和模式串判断即可,为了达到循环的效果,要把原串写两遍.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>#define MAX 200007using namespace std;int t;char s[MAX],p[MAX];int Next[MAX],extend[MAX];void getNext ( ){    memset ( Next , 0 , sizeof ( Next ) );    int i , len = strlen(p);    Next[0] = len;    for ( i = 0; i < len-1 && p[i] == p[i+1]; i++ );    Next[1] = i;    int a = 1;    for ( int k = 2; k < len ; k++ )    {        int b = a+Next[a]-1 , l = Next[k-a];        if ( (k-1)+l  >= b )        {            int j = (b-k+1)>0?b-k+1:0;                while ( k+j < len && p[k+j] == p[j] ) j++;            Next[k] = j , a = k;        }        else Next[k] = l;    }}void getExtend ( ){    getNext ( );    int slen = strlen ( s ) , tlen = strlen ( p ) , a = 0;    int mlen = min ( slen , tlen );    while ( a < mlen && s[a] == p[a] ) a++;    extend[0] = a , a = 0;    for ( int k = 1; k < slen ; k++ )    {        int b = a+extend[a]-1 , l = Next[k-a];        if ( (k-1)+l >= b )        {            int j = (b-k+1)>0?b-k+1:0;            while ( k+j < slen && j<tlen && s[k+j]==p[j] ) j++;            extend[k] = j;            a = k;        }        else extend[k] = l;    }}void solve ( int c ){    int l,g;    l = g = 0;    int len = strlen (p);    int temp = len;    for ( int i = 1 ; i < len ; i++ )        if ( Next[i] == len-i && !(len%i) )         {            temp = i;            break;        }    for ( int i = 0 ; i < temp ; i++ )    {       if ( extend[i] >= len ) continue;       else        {           if ( s[i+extend[i]] > p[extend[i]] ) g++;           else l++;       }    }    printf ( "Case %d: %d 1 %d\n" , c , l , g );}int main ( ){    scanf ( "%d" , &t );    int c = 1;    while ( t-- )    {        scanf ( "%s" , p );        int len = strlen ( p );        for ( int i = 0 ; i < len ; i++ )            s[i] = s[i+len] = p[i];        s[2*len] = 0;        getExtend();        solve ( c++ );    }}


 
0 0
原创粉丝点击