hdu4333

http://www.elijahqi.win/2017/07/07/hdu4333/
hdu4333 扩展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

Source
2012 Multi-University Training Contest 4
题解

将 字符串重新粘贴到原字符串后面 整体求 exkmp 最后要求求出 有多少比原数大 比原数小 和原数字相同的 注意需要 最后答案除一下循环节

#include <cstdio>
#include <cstring>
#define N 220000
char str1[N],str2[N>>1];
int num,next[N>>1],e[N],L1,L2,equal,min,max,T;
void getnext(){
    next[0]=L2;
    for (int i=1;i<L2;++i) {
        if (str2[i]==str2[i-1]) next[1]++;else break;
    }
    int a=1,p=next[1]-1;
    for (int i=2;i<L2;++i){
        int l1,l2;
        l1=next[i-a];l2=p-i+1;
        if (l1<l2) next[i]=l1;else{
            int j=l2;
            if (j<0) j=0;
            while (i+j<L2&&str2[i+j]==str2[j])++j;
            next[i]=j;p=i+j-1;a=i;
        }
    }
}
void getextend(){
    for (int i=0;i<L1;++i){
        if (i<L2&&str2[i]==str1[i]) e[0]++;else break;
    }
    //printf("%d %d",L1,L2);
    //printf("%d",e[0]);
    int p=e[0]-1,a=0;
    for (int i=1;i<L1;++i){
        int l1=next[i-a],l2=p-i+1;
        if (l1<l2) e[i]=l1;else{
            int j=l2;
            if (j<0) j=0;
            while (j<L2&&i+j<L1&&str1[i+j]==str2[j])++j;
            e[i]=j;p=i+j-1;a=i;
        }
    }
}
void makenext(){
    next[0]=0;//q:模版字符串下标;k:最大前后缀长度
    for (int q=1,k=0;q<L2;q++){
        while (k>0&&str2[q]!=str2[k]) k=next[k-1];
        if (str2[q]==str2[k]){
            ++k;
        }
        next[q]=k;
    }
}
int main(){
    freopen("hdu4333.in","r",stdin);
    freopen("hdu4333.out","w",stdout);
    scanf("%d",&T);int T1=T;
    while (T--){
        scanf("%s",str1);num=strlen(str1);
        max=min=equal=0;memset(e,0,sizeof(e));memset(next,0,sizeof(next));
        for (int i=0;i<num;++i) str1[num+i]=str1[i]; //printf("%s\n",str1);
        for (int i=0;i<num;++i) str2[i]=str1[i];//printf("%s",str2);
        L1=num<<1;L2=num;//printf("%d %d",L1,L2);
        getnext();//for (int i=0;i<L2;++i) printf("%d ",next[i]);printf("\n");
        getextend();memset(next,0,sizeof(next));
        makenext();
    //  for (int i=0;i<L1;++i) printf("%d ",e[i]);
        //NextVal(str2);
        int tmp=(L2%(L2-next[L2-1])==0)?L2/(L2-next[L2-1]):1;
        //printf("%d",tmp);
    //  for (int i=0;i<L2;++i) printf("%d ",next[i]);
        //printf("%d ",num);
        for (int i=0;i<num;++i){
            if (e[i]>=num) {
                equal++;continue;
            }
            if (str1[i+e[i]]>str2[e[i]]) max++;else min++;
            //printf("%d ",e[i]);
        }
        printf("Case %d: %d %d %d\n",T1-T,min/tmp,equal/tmp,max/tmp);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值