zoukankan      html  css  js  c++  java
  • Good Bye 2015 D. New Year and Ancient Prophecy

    time limit per test
    2.5 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    Limak is a little polar bear. In the snow he found a scroll with the ancient prophecy. Limak doesn't know any ancient languages and thus is unable to understand the prophecy. But he knows digits!

    One fragment of the prophecy is a sequence of n digits. The first digit isn't zero. Limak thinks that it's a list of some special years. It's hard to see any commas or spaces, so maybe ancient people didn't use them. Now Limak wonders what years are listed there.

    Limak assumes three things:

    • Years are listed in the strictly increasing order;
    • Every year is a positive integer number;
    • There are no leading zeros.

    Limak is going to consider all possible ways to split a sequence into numbers (years), satisfying the conditions above. He will do it without any help. However, he asked you to tell him the number of ways to do so. Since this number may be very large, you are only asked to calculate it modulo 109 + 7.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — the number of digits.

    The second line contains a string of digits and has length equal to n. It's guaranteed that the first digit is not '0'.

    Output

    Print the number of ways to correctly split the given sequence modulo 109 + 7.

    Sample test(s)
    input
    6
    123434
    
    output
    8
    
    input
    8
    20152016
    
    output
    4
    
    Note

    In the first sample there are 8 ways to split the sequence:

    • "123434" = "123434" (maybe the given sequence is just one big number)
    • "123434" = "1" + "23434"
    • "123434" = "12" + "3434"
    • "123434" = "123" + "434"
    • "123434" = "1" + "23" + "434"
    • "123434" = "1" + "2" + "3434"
    • "123434" = "1" + "2" + "3" + "434"
    • "123434" = "1" + "2" + "3" + "4" + "34"

    Note that we don't count a split "123434" = "12" + "34" + "34" because numbers have to be strictly increasing.

    In the second sample there are 4 ways:

    • "20152016" = "20152016"
    • "20152016" = "20" + "152016"
    • "20152016" = "201" + "52016"
    • "20152016" = "2015" + "2016"

    这题可以用dp做,设dp[i][j]表示以i.....j这一串数字结尾的符合条件分割方法的方案数,那么我们最后要求的就是dp[k][n],k=1...n的和。当s[i][j]=='0'的时候,dp[i][j]一定是0,所以可以跳过。我们可以枚举每一对(i,j),然后对于(i,j)找到前面最小的位置p,使得(p,i-1)组成的数字比(i,j)严格小,可以观察发现,如果(p,i-1)的长度小于(i,j),一定成立,如果大于(i,j),那么就是前面有很多0的情况,其实我们可以忽略这些0,因为这些0不能和后面的数组成为一个新的数(分割的数不能含前导0),所以只需要考虑相等的情况。因为这两个数很大,要判断哪个比较大,我们要找到两个数从第一位开始第一个不相同的数,然后比较,当然也有可能完全相等,这个过程要花O(n)的时间,那么有没有更快的方法呢?有的,我们可以预处理出nxt[a][b]表示最小的p使得s[a+p]!=s[b+p],那么nxt[a][b]=0或者nxt[a][b]=nxt[a+1][b+1]+1.当我们处理出最小的p时,我们就得到一个有效区间(p,i-1)表示任意的k,p=<k<=i-1,(k,i-1)作为(i,j)前一个数是符合小于(i,j)这个数的,那么我们需要累加(k,i-1),p=<k<=i-1,这里我们可以开个二维数组来记录和。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef __int64 ll;
    #define inf 99999999
    #define pi acos(-1.0)
    #define MOD 1000000007
    #define maxn 5050
    ll dp[maxn][maxn];
    int nxt[maxn][maxn];
    ll c[maxn][maxn];
    char s[maxn];
    
    int panduan(int x,int y,int xx,int yy)
    {
        int i,j,p;
        p=nxt[x][xx];
        if(p<=y-x){
            if(s[x+p]>=s[xx+p]){
                return 1;
            }
            return 0;
        }
        else return 1;
    
    }
    
    int main()
    {
        int n,m,i,j,l,r,p;
        while(scanf("%d",&n)!=EOF)
        {
            scanf("%s",s+1);
            //memset(dp,0,sizeof(dp));
            //memset(nxt,0,sizeof(nxt));
            for(i=1;i<=n;i++){
                if(s[i]==s[n]){
                    nxt[i][n]=nxt[n][i]=1;
                }
                else nxt[i][n]=nxt[n][i]=0;
            }
            for(i=n-1;i>=1;i--){
                for(j=n-1;j>=1;j--){
                    if(s[i]!=s[j]){
                        nxt[i][j]=0;
                    }
                    else{
                        nxt[i][j]=nxt[i+1][j+1]+1;
                    }
                }
            }
            for(j=1;j<=n;j++){
                for(i=1;i<=j;i++){
                    if(s[i]=='0')continue;
                    if(i==1){
                        dp[i][j]=1;continue;
                    }
                    p=2*i-j-1;
                    if(p<1){
                        p=1;
                    }
                    else{
                        if(panduan(p,i-1,i,j)){
                            p++;
                        }
    
                    }
                    if(p>=1 && p<=i-1){
                        dp[i][j]=c[p][i-1]%MOD;
                    }
                }
                c[j][j]=dp[j][j]%MOD;
                for(i=j-1;i>=1;i--){
                    c[i][j]=(c[i+1][j]+dp[i][j])%MOD;
                }
    
            }
            ll ans=0;
            for(i=1;i<=n;i++){
                ans=(ans+dp[i][n])%MOD;
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }


  • 相关阅读:
    正则表达式 ^
    jQuery的加法运算,val()获取的结果相加变成了字符串连接。
    mssql 取数据指定条数(例:100-200条的数据)
    css样式大全(整理版)
    50个技巧提高你的PHP网站程序执行效率
    ASP版_阿里大于短信API Demo
    FusionCharts的使用方法(超详细)
    FusionCharts参数说明 (中文)
    web服务器选择Apache还是Nginx
    反向代理服务器的工作原理
  • 原文地址:https://www.cnblogs.com/herumw/p/9464586.html
Copyright © 2011-2022 走看看