【Codeforces Good Bye 2015】D. New Year and Ancient Prophecy
2016-01-20 22:55
483 查看
Problem
Time limit per test 2.5 secondsMemory 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)
input6
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时,可以把转移表写出
接下来
4000
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 ; ll dp , f ; bool ok ; int height ; 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 [i]) % mod; cout << ans << endl; } void init() { n = read(); rep(i, 0, n - 1) { char ch = getchar(); s[i] = ch - '0'; } s = 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。。
End.
相关文章推荐
- linux查看进程的运行路径
- 【Linux】之系统工具top
- linux查看进程的启动时间
- 服务器端架构,前端服务器与客户端随机负载平衡
- Xshell下漂亮的开发环境配置
- linux根据端口号找出进程名
- Ubuntu下安装JDK8和TOMCAT
- Linux Mac之间文件传输
- nginx&tomcat with keepalived for configing cluster
- CentOs中JDK与tomcat配置
- Tomcat项目部署方式--利用tomcat发布网站
- linux查找被删除但是未释放空间的文件
- linux下简单的测试cpu性能的方法
- Linux下修改MySQL的root用户密码
- The working copy xxxx needs to be upgraded to Subversion 1.7.
- OpenGL超级宝典7th简体中文-第五章-数据
- 基于tiny4412的Linux内核移植(支持device tree)(三)
- 运维堡垒机----Gateone
- 关于如何将yaffs2源码打包入linux源码中
- linux 学习之路