codeforces Good Bye 2015 D. 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.

Examples
input
6
123434
output
8

题意:给你一串字符串(都是数字),叫你判断有多少种方式可以拆分成连续上升的若干个数字。(不含前导0)

 

很容易想到dp[i][j]i表示现在到了第几个数字,j表示到了这个数字的时候最后一个数字有几位

ans[i][j]表示的是到了i,最后一个数字取小于等于j位的方案数。

Lcp[i][j]表示i开始的字符串和j开始的字符串的最长公共前缀。(因为进行累加的时候要进行字符串的比较,可以提前预处理出字符串的相同的位数)

那么每一个ij位的时候都可能有以下几种情况

1.s[i-j+1]=='0',表示取j位是不合法的数字

2.i-j<j,表示i-j之前的位数小于j,那么便可以直接加上ans[i-j][i-j],因为i-j之前可能的数字的位数小于j,而现在的数字位数是j

3.s[i-2*j+1]=='0'表示i-j取之前j位产生的数字不合法

4.Lcp[i-2*j+1][i-j+1]>=j,表示i-2*j+1开始的字符串和i-j+1开始的字符串的最长前缀大于等于j,即表示i取之前j位不大于i-j取之前j

5.否则的话比较i-2*j+1开始的字符串和i-j+1开始的字符串第一位不同的字符

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
const int maxn=5100;
const int MOD=1e9+7;
char s[maxn];
int ans[maxn][maxn],lcp[maxn][maxn];

void cal(int &x,int y){
    x+=y;
    if(x>=MOD)
        x%=MOD;
}

int main(){
    int n;
    scanf("%d",&n);
    scanf("%s",s+1);
    for(int i=n;i>=1;i--)
        for(int j=n;j>=1;j--)
            if(s[i]==s[j])
                lcp[i][j]=lcp[i+1][j+1]+1;//以i为开头和以j为开头的字符串的相同位置
    ans[0][0]=1;
    for(int i=1;i<=n;i++)//枚举到了哪一位
        for(int j=1;j<=i;j++){//枚举前面有几位
            cal(ans[i][j],ans[i][j-1]);
            if(s[i-j+1]=='0')
                continue;
            if(i-j<j){
                cal(ans[i][j],ans[i-j][i-j]);
                continue;
            }
            if(s[i-2*j+1]=='0'){
                cal(ans[i][j],ans[i-j][j]);
                continue;
            }
            if(lcp[i-2*j+1][i-j+1]>=j){
                cal(ans[i][j],ans[i-j][j-1]);
                continue;
            }
            if(s[i-2*j+1+lcp[i-2*j+1][i-j+1]]<s[i-j+1+lcp[i-2*j+1][i-j+1]])
                cal(ans[i][j],ans[i-j][j]);
            else
                cal(ans[i][j],ans[i-j][j-1]);
        }
    printf("%d\n",ans[n][n]);
    return 0;
}

你可能感兴趣的:(codeforces Good Bye 2015 D. New Year and Ancient Prophecy)