zoukankan      html  css  js  c++  java
  • hdu4333 扩展KMP

    Revolving Digits

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


    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求所有后缀与原串的最长公共前缀(方便比较大小),最后再做一个KMP去循环节(不同的数)
     
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    
    int tt,t,x,z,q,e,i,j,n;
    int s[300011],a[300011],next[300011];
    char c;
    
    using namespace std;
    
    void Read()
    {
        n=0;
        while(c=getchar(),c<'0'||c>'9');
        s[++n]=c;
        while(c=getchar(),c>='0'&&c<='9')s[++n]=c;
    }
    
    void Exkmp()
    {
        int i,len,l,k;
        len=0;
        for(i=2;i<=n;i++){
            if(len<i){
                l=0;
                while(s[i+l]==s[1+l]&&i+l<=n)l++;
                a[i]=l;
            }
            else{
                l=a[i-k+1];
                if(l<len-i+1)a[i]=l;
                else{
                    l=len-i+1;
                    while(s[i+l]==s[1+l]&&i+l<=n)l++;
                    a[i]=l;
                }
            }
            if(i+a[i]-1>len){
                len=i+a[i]-1;
                k=i;
            }
        }
        x=0;
        z=1;
        q=0;
        for(i=2;i<=n/2;i++){
            if(a[i]>=n/2)z++;
            else{
                if(s[i+a[i]]>s[1+a[i]])q++;
                if(s[i+a[i]]<s[1+a[i]])x++;
            }
        }
    }
    
    void Kmp()
    {
        int i,x;
        memset(next,0,sizeof(next));
        x=0;
        for(i=2;i<=n/2;i++){
            while(x!=0&&s[i]!=s[x+1])x=next[x];
            if(s[i]==s[x+1])x++;
            next[i]=x;
        }
    }
    
    int main()
    {
        scanf("%d",&t);
        for(tt=1;tt<=t;tt++){
            memset(s,0,sizeof(s));
            memset(a,0,sizeof(a));
            Read();
            for(i=1;i<=n;i++)s[i+n]=s[i];
            i+=n;
            n=n*2;
            Exkmp();
            Kmp();
            n=n/2;
            if(n%(n-next[n])==0){
                e=n/(n-next[n]);
                x/=e;
                z/=e;
                q/=e;
            }
            printf("Case %d: %d %d %d
    ",tt,x,z,q);
        }
    }
  • 相关阅读:
    tar.gz文件
    Ruby Symbol
    Ruby表达式
    Linux相关命令
    Ruby file
    Tomcat优化
    修改Linux文件的所属用户和组
    Ruby String
    Ruby Range
    HTML5开源专业图像处理引擎——AlloyImage(简称AI)
  • 原文地址:https://www.cnblogs.com/applejxt/p/3801784.html
Copyright © 2011-2022 走看看