Problem
Time limit per test 2.5 seconds
Memory limit per test 512 megabytes
Input standard input
Output standard output
Description
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”
Solution
Skill : 对于一个给定的字符串,两段相邻的子串的LCP是可以递推的
方法如下图所示
我们注意到,当两个个长度为4的相邻子串左移一位时,除去首尾,中间的对应方式和左移前是完全相同的,所以只需要比对第一位,就可以递推LCP。这个过程的复杂度是
O(n2)
的。
在递推LCP时,可以把转移表写出
接下来
O(n2)
DP即可
Code
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define red(i, a, b) for(int i = (a); i >= (b); i--)
#define ll long long
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(!isdigit(c)) { if (c == '-') f = -1; c = getchar(); }
while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
const int N = 5010;
const ll mod = 1000000007ll;
int n;
int s[N];
ll dp[N][N], f[N][N];
bool ok[N][N];
int height[N][N];
int check(int l1, int r1, int l2, int r2) {
rep(i, l1, r1) if (s[i] != s[l2 + i - l1]) return i - l1;
return r1 - l1 + 1;
}
void Work() {
memset(dp, 0, sizeof(0));
memset(f, 0, sizeof(f));
if (s[1] == 0) dp[1][0] = 0;
else dp[1][0] = 1;
f[1][0] = dp[1][0];
rep(i, 2, n) {
if (s[i] == 0) continue;
rep(j, 0, i - 1) {
if (j == 0) {
dp[i][j] = 1;
f[i][j] = 1;
continue;
}
f[i][j] = f[i][j - 1];
int lim = 2 * j - i;
if (lim >= 0) dp[i][j] = (f[j][j - 1] - f[j][lim] + mod) % mod;
else dp[i][j] = f[j][j - 1];
if (2 * j - i >= 0) {
if (ok[i - 1][j - 1]) dp[i][j] = (dp[i][j] + dp[j][2 * j - i]) % mod;
}
f[i][j] = (f[i][j] + dp[i][j]) % mod;
}
}
ll ans = 0;
rep(i, 0, n - 1) ans = (ans + dp[n][i]) % mod;
cout << ans << endl;
}
void init() {
n = read();
rep(i, 0, n - 1) {
char ch = getchar();
s[i] = ch - '0';
}
s[n] = 10;
memset(height, 0, sizeof(height));
rep(len, 1, (n + 1) / 2) {
red(i, n - 1, len * 2 - 1) {
int l1 = i - len * 2 + 1, l2 = i - len + 1, tmp;
if (i == n - 1) height[i][l2 - 1] = check(l1, l2 - 1, l2, i);
else height[i][l2 - 1] = s[l2] != s[l1] ? 0 : min(height[i + 1][l2] + 1, len);
tmp = height[i][l2 - 1];
if (tmp == len) ok[i][l2 - 1] = 0;
else if (s[l1 + tmp] < s[l2 + tmp]) ok[i][l2 - 1] = 1;
else ok[i][l2 - 1] = 0;
}
}
}
int main() {
init();
if (s[0] == 0) printf("0\n");
else Work();
return 0;
}
尾声
这个递推是多么的简单轻巧
我和SR却都不会-_-||
期末考试考完了,九连炸,boom
HBH就是大
GJ更大
SR。。