HDU 4333(扩展KMP)

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

题目大意:给你一个数字,然后将这个数字的每一位都向前移动一位,此时的第一位放到最后一位,问这样得到的数字中有多少个数字是小于原数字,多少个数字是等于原数字,多少个数字是大于原数字的。

解题思路:将这个数字复制一串放到原序列后面,那么问题就转换为以第i位开始的长度为n的字符串与原字符串的长度,这里我们介绍一个算法:扩展KMP算法,主要解决的问题是,有两个字符串t,s,它要求的是字符串s的后缀与字符串t的前缀相等的最大长度。

扩展KMP算法使用的主要是manacher算法的思想:
我们用数组extend[i] 表示s[i]…s[n]与t的前缀相等的最大长度。假设当前遍历到s串的位置i,即我们已经算出extend[0]…extend[i-1],且我们知道s串中与t串的前缀匹配的一个最右边的子串的起始位置和结束位置为a,p,即s[a…p) =t[0…p-a)。然后我们在定义一个数组next[i]表示t[i]…t[m-1]与t的最长相同前缀。即t为自身的extend数组。然后此时我们知道了s[a…p)等于t[0…p-a),那么如果i是小于p,就表示i包含在和t数组匹配的子串中,如果此时i+next[i]小于p,next[i],表示串t的后缀与前缀相等的最大长度,那么extend[i]就等于next[i],否则,就继续匹配即可。

代码:

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <bitset>
#include <queue>
//#include <random>
#include <time.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ls root<<1
#define rs root<<1|1
const int inf=1ll*1<<60;
const int maxn=2e6+10;
const int maxm=3e6+10;
char arr[maxn];
int to[maxn],extend[maxn];
void getextend()
{
    int a=0,p=0;
    extend[0]=strlen(arr);
    for(int i=1;arr[i]!='\0';i++){
        if(i>=p || i+extend[i-a]>=p){
            if(i>=p) p=i;
            while(arr[p]!='\0' && arr[p]==arr[p-i]) p++;
            extend[i]=p-i;
            a=i;
        }
        else extend[i]=extend[i-a];
    }
}
void kmp() {
	int t=-1;
	to[0]=-1;
	for(int i=1; arr[i]!='\0'; i++) {
		t=to[i-1];
		while(t!=-1 && arr[t+1]!=arr[i]) {
			t=to[t];
		}
		if(arr[t+1]==arr[i])to[i]=t+1;
		else to[i]=-1;
	}
}
signed main() {
    int t,test=1;
    scanf("%lld",&t);
    while(t--){
        scanf("%s",arr);
        int l=0,e=0,g=0,len=strlen(arr);
        for(int i=0;i<len;i++)arr[i+len]=arr[i];
        arr[len+len]='\0';
        getextend();
        for(int i=0;i<len;i++){
            if(extend[i]>=len)e++;
            else{
                int x=extend[i];
                if(arr[x]>arr[x+i])l++;
                else g++;
            }
        }
        kmp();
        int res=1,tmp=len-to[len-1]-1;
		if(len%tmp==0) {
			res=len/tmp;
		}
        printf("Case %lld: %lld %lld %lld\n",test++,l/res,e/res,g/res);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值