CodeForces 611D New Year and Ancient Prophecy

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"

恩,这道题第一眼看就赶脚好难不会写,就先补E题,回头又看这题,反复看了好久人家的题解才懂。。。。

dp[ i][ j] 表示以第i个位置为结尾的最后一块长度为 j 的字符的分法数,然后 dp [ i][ j] +=dp[ i-j][k]( 1 <=k <=j-1)因为当k处于此范围内时数的长度都小于j 值肯定也小(这里用一二维数组记录和),当长度相同时就要比较第一个不同的数字的大小若小于 dp [ i][ j] +=dp[ i-j ][ j],然后这里比较第一个不同的字符时,用一二维数组记录 s[ i + p] != s[ j + p ] 中 p 的最小值,当 s[ i ] !=s[ j] 时p=0,相等时cnt[ i ][ j]= cnt [ i+1 ][ j+1 ] +1


#include <iostream>
#include<cstdio>
#include<cstring>
#define maxn 5010
#define LL __int64
using namespace std;

int dp[maxn][maxn];
int cnt[maxn][maxn];
int sum[maxn][maxn];
char s[maxn];
int MM=1e9+7;

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        scanf("%s",s+1);
        memset(cnt,0,sizeof(cnt));
        for(int i=n;i>0;--i)
        {
            for(int j=n;j>i;--j)
            {
                if(s[i]!=s[j])
                    cnt[i][j]=0;
                else
                    cnt[i][j]=cnt[i+1][j+1]+1;
            }
        }
        memset(dp,0,sizeof(dp));
        memset(sum,0,sizeof(sum));
        dp[0][0]=1;
        for(int i=0;i<=n;++i)
            sum[0][i]=1;
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=i;++j)
            {
                if(s[i-j+1]=='0')
                    continue;
                dp[i][j]=(dp[i][j]+sum[i-j][j-1])%MM;
                int l=i-j-j+1;
                int r=i-j+1;
                if(l<1)
                    continue;
                if(cnt[l][r]<j&&s[l+cnt[l][r]]<s[r+cnt[l][r]])
                    dp[i][j]=(dp[i][j]+dp[i-j][j])%MM;
            }
            for(int j=1;j<=n;++j)
                sum[i][j]=(sum[i][j-1]+dp[i][j])%MM;
        }
        LL ans=0;
        for(int i=1;i<=n;++i)
            ans=(ans+dp[n][i])%MM;
        printf("%I64d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值