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.
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'.
Print the number of ways to correctly split the given sequence modulo 109 + 7.
6 123434
8
8 20152016
4
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"
题意是给出一个数字字符串,问能拆成多少种严格递增的数列。不包括前置0。
dp[n][k]表示第n个位置,最后一个数字长度为k时的拆分个数。
有dp[i][j]=sum(dp[i-j][k])(1<=k<=j-1) + dp[i-j][j]//当前者的数比后者的数小时
当位数相同时,如何比较前者的数和后者的数?记录最长公共前缀,然后比较最长公共前缀之后的那个字符即可。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <ctime>;
#include <set>
#include <map>
using namespace std;
#define INF 0x3fffffff
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 5005;
int n;
char val[maxn];
int cnt[maxn][maxn], pre_sum[maxn][maxn], dp[maxn][maxn], sum[maxn][maxn];
void lcp()
{
int i, j;
for (i = n; i >= 1; i--)
{
for (j = n; j > i; j--)
{
if (val[i] == val[j])
{
cnt[i][j] = cnt[i + 1][j + 1] + 1;
}
else
{
cnt[i][j] = 0;
}
}
}
}
void solve()
{
scanf("%d", &n);
scanf("%s", val + 1);
lcp();
int i, j;
for (i = 1; i <= n; i++)dp[i][i] = 1;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
if (val[i - j + 1] == '0')continue;
dp[i][j] = (dp[i][j] + pre_sum[i - j][j - 1]) % mod;
if (i - 2 * j + 1>= 1 && cnt[i - 2 * j + 1][i - j + 1] < j&&val[i - 2 * j + 1 + cnt[i - 2 * j + 1][i - j + 1]] < val[i - j + 1 + cnt[i - 2 * j + 1][i - j + 1]])
{
dp[i][j] = (dp[i][j] + dp[i - j][j]) % mod;
}
}
for (j = 1; j <= n; j++)//求前缀和这里一定要到n
{
pre_sum[i][j] = (pre_sum[i][j - 1] + dp[i][j]) % mod;
}
}
ll ans = 0;
for (i = 1; i <= n; i++)
{
ans = (ans + dp[n][i]) % mod;
}
printf("%I64d", ans);
}
int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout);
solve();
return 0;
}