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 Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值