zoukankan      html  css  js  c++  java
  • HDU 4333 Revolving Digits (扩展KMP)

    Revolving Digits

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


    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
     
    Recommend
    zhoujiaqi2010
     
     
    扩展KMP能求出一个串所有后缀串(即s[i...len])和模式串的最长公共前缀。于是只要将这个串复制一遍,求出该串每个后缀与其本身的最长公共前缀即可,当公共前缀>=len时,显然相等,否则只要比较下一位就能确定这个串与原串的大小关系。

      至于重复串的问题,只有当这个串有循环节的时候才会产生重复串,用KMP的next数组求出最小循环节,用长度除以最小循环节得到循环节个数,在将3个答案都除以循环节个数即可。

    //============================================================================
    // Name        : HDU4333.cpp
    // Author      : 
    // Version     :
    // Copyright   : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================
    
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <stdio.h>
    using namespace std;
    const int MAXN=2000010;
    
    int next[MAXN];
    int extend[MAXN];
    
    void EKMP(char s[],char t[])//s为主串,t为模板串
    {
        int i,j,p,L;
        int lens=strlen(s);
        int lent=strlen(t);
        next[0]=lent;
        j=0;
        while(j+1<lent && t[j]==t[j+1])j++;
        next[1]=j;
    
        int a=1;
        for(i=2;i<lent;i++)
        {
            p=next[a]+a-1;
            L=next[i-a];
            if(i+L<p+1)next[i]=L;
            else
            {
                j=max(0,p-i+1);
                while(i+j<lent && t[i+j]==t[j])j++;
                next[i]=j;
                a=i;
            }
        }
    
        j=0;
        while(j<lens && j<lent && s[j]==t[j])j++;
        extend[0]=j;
        a=0;
        for(i=1;i<lens;i++)
        {
            p=extend[a]+a-1;
            L=next[i-a];
            if(L+i<p+1)extend[i]=L;
            else
            {
                j=max(0,p-i+1);
                while(i+j<lens && j<lent && s[i+j]==t[j])j++;
                extend[i]=j;
                a=i;
            }
        }
    }
    
    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];
        }
    }
    char str1[MAXN],str2[MAXN];
    int main()
    {
        int T;
        int iCase=0;
        scanf("%d",&T);
        while(T--)
        {
            iCase++;
            scanf("%s",str1);
            int len=strlen(str1);
            strcpy(str2,str1);
            strcat(str2,str1);
            EKMP(str2,str1);
            int cnt1=0,cnt2=0,cnt3=0;
    
            for(int i=0;i<len;i++)
            {
                if(extend[i]>=len)cnt2++;
                else
                {
                    if(str2[i+extend[i]]<str1[extend[i]])cnt1++;
                    else cnt3++;
                }
            }
            getNext(str1,len);
            int t=len-next[len];
            int tol=1;
            if(len%t==0)tol=len/t;
            printf("Case %d: %d %d %d\n",iCase,cnt1/tol,cnt2/tol,cnt3/tol);
        }
        return 0;
    }
    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    第二阶段--团队冲刺--第八天
    第二阶段--团队冲刺--第七天
    第二阶段--团队冲刺--第六天
    ShardingJdbc:Springboot集成ShardingSphere,单服务跨数据源时,简单实现事务管理
    ShardingJdbc:集成ShardingSphere后,像MasterDataTable这样不需要分库分表,该怎么实现的一个方案
    小知识:mybatisGeneratorConfig的xml 数据库连接URL报错:&符号报错 connectionURL="jdbc:mysql://localhost:3306/mcspcsales?charset=utf8mb4&amp;useSSL=false&amp;serverTimeZone=GMT%2B8"
    小知识:如何在idea中执行mybatis-generator,生成mapper
    小知识:idea新建项目后,希望通过actuator查询当前是否正常,发现前台画面显示whiteablepage
    小知识:windows安装mysql报错,安装mysql时提示错误: C:mysql-5.7.29-winx64in>mysqld -install MySQL Install/Remove of the Service Denied!
    小知识:nginx转发访问时,报404错误
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2763242.html
Copyright © 2011-2022 走看看