hdu 4333 扩展KMP

点击打开链接


Revolving Digits

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


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
 
问一个数字,旋转后比它大的?等的?小的?
应用扩展KMP,先扩展字符到两倍长度,然后求其本身最长前缀,即比较不同的哪一位即可,
即比较:str[i+next[i]]与str[next[i]]的大小

#include<iostream>
#include<stdio.h>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<string.h>
#include<algorithm>
#define N 4100000
#define mod 10007
#define ll long long
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define ex 2.7182818284590452354+
#define pi 3.141592653589793239
#define INFF 999999999
using namespace std;
char str[N];
int next[N];
int n;
void getnext()
{
    int a,p,i,j=-1;
    next[0]=0;
    for(i=1;i<n;i++,j--)
    {
        if(j<0||i+next[i-a]>=p)
        {
            if(j<0)
                j=0,p=i;
            while(p<n&&str[j]==str[p])
                p++,j++;
            next[i]=j;
            a=i;
        }
        else
            next[i]=next[i-a];
    }
}
int main()
{
    int t;
    while(scanf("%d",&t)!=EOF)
    {
        int i,xx=1;
        while(t--)
        {
            scanf("%s",str);
            int mn,m,mx;
            n=m=strlen(str);
            for(i=0;i<m;i++)
                str[n++]=str[i];
            str[n]='\0';
            mx=0,mn=0;
            getnext();
            for(i=0;i<n;i++)
            {
                if(next[i]>=m)
                    break;
                if(str[i+next[i]]>str[next[i]])
                    mx++;
                if(str[i+next[i]]<str[next[i]])
                    mn++;
            }
            printf("Case %d: ",xx++);
            printf("%d 1 %d\n",mn,mx);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值