Good Bye 2015 D. New Year and Ancient Prophecy
2.5 seconds
512 megabytes
standard input
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.
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"
一个长为n的数字字符串,划分成若干子串,保证按照递增顺序的方案数。
f[i][j]表示以i下标为结尾,i所在的子串长度不超过j的方案数。可以得到如下递推式:
f[i][j]=f[i][j-1], s[i-j]=='0'
f[i-j][i-j], i-2*j<0
f[i-j][j], s[i-2*j...i-j-1]<s[i-j...i-1]
f[i-j][j-1],s[i-2*j...i-j-1]>=s[i-j...i-1]
那么对于第3、4种情况,需要判定两段字符串的大小,如果从头到尾判断的话,复杂度为O(n),对于极限数据来说会超时。
解决方法有两种:
1. 将s的所有子串哈希,对于两个起始位置x、y,二分找到最小的不相等的长度,然后比较,复杂度为O(logn)
2. 设low[x][y]为:对于两个起始位置x、y的最小的不相等的长度。则有如下递推式:
low[i][j]= 0, s[i-1]!=s[j-1]
low[i+1][j+1]+1, s[i-1]==s[j-1]
然后直接比较x+low,y+low位,复杂度为O(1)
1. 哈希
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <ctime>
#define oo 1000000007
#define maxn 5200
#define maxm 4200 using namespace std; int n;
string s;
int f[maxn][maxn];
long long hash[maxn][maxn];
//f[x][y] 表示以x结尾的长度小于等于y的答案 bool judge(int x,int y,int len)
{
int l=,r=len-,d=;
while (l<=r)
{
int mid=(l+r)>>;
if (hash[x][x+mid]!=hash[y][y+mid])
{
r=mid-;
d=mid;
}
else
l=mid+;
}
return s[x+d-]<s[y+d-];
} int main()
{
//freopen("D.in","r",stdin);
scanf("%d",&n);
cin>>s;
memset(hash,,sizeof(hash));
for (int i=;i<=n;i++)
for (int j=i;j<=n;j++)
hash[i][j]=hash[i][j-]*+s[j-];
f[][]=;
for (int i=;i<=n;i++)
{
for (int j=;j<=i;j++)
{
if (s[i-j]=='')
{
f[i][j]=f[i][j-];
continue;
}
int p;
//p是前一段可以取得最长长度
if (i-*j>=)
{
if (judge(i-*j+,i-j+,j)) p=j;
else p=j-;
}
else
p=i-j;
f[i][j]=(f[i][j-]+f[i-j][p])%oo;
}
}
printf("%d\n",f[n][n]);
return ;
}
hash
2.dp
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <ctime>
#define oo 1000000007
#define maxn 5200
#define maxm 4200 using namespace std; int n;
string s;
int f[maxn][maxn],low[maxn][maxn];
//f[x][y] 表示以x结尾的长度小于等于y的答案
//low[x][y] 表示从x、y开始的最小的不相等的偏移量 bool judge(int x, int y, int len)
{
int d=low[x][y];
return d<len && s[x+d-]<s[y+d-];
} int main()
{
//freopen("D.in","r",stdin);
scanf("%d",&n);
cin>>s;
for (int i=n;i>=;i--)
for (int j=n;j>i;j--)
if (s[i-]!=s[j-])
low[i][j]=;
else
low[i][j]=low[i+][j+]+;
f[][]=;
for (int i=;i<=n;i++)
for (int j=;j<=i;j++)
{
if (s[i-j]=='')
{
f[i][j]=f[i][j-];
continue;
}
int p;
//p是前一段可以取得最长长度
if (i-*j>=)
{
if (judge(i-*j+,i-j+,j)) p=j;
else p=j-;
}
else
p=i-j;
f[i][j]=(f[i][j-]+f[i-j][p])%oo;
}
printf("%d\n",f[n][n]);
return ;
}
dp
Good Bye 2015 D. New Year and Ancient Prophecy的更多相关文章
- Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp
D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...
- 【27.34%】【codeforces 611D】New Year and Ancient Prophecy
time limit per test2.5 seconds memory limit per test512 megabytes inputstandard input outputstandard ...
- Good Bye 2015 B. New Year and Old Property 计数问题
B. New Year and Old Property The year 2015 is almost over. Limak is a little polar bear. He has re ...
- Good Bye 2015 A. New Year and Days 签到
A. New Year and Days Today is Wednesday, the third day of the week. What's more interesting is tha ...
- Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP
B. New Year and Old Property 题目连接: http://www.codeforces.com/contest/611/problem/B Description The y ...
- Codeforces Good Bye 2015 A. New Year and Days 水题
A. New Year and Days 题目连接: http://www.codeforces.com/contest/611/problem/A Description Today is Wedn ...
- Good Bye 2015 B. New Year and Old Property —— dfs 数学
题目链接:http://codeforces.com/problemset/problem/611/B B. New Year and Old Property time limit per test ...
- codeforces Good Bye 2015 B. New Year and Old Property
题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...
- Good Bye 2015 C. New Year and Domino 二维前缀
C. New Year and Domino They say "years are like dominoes, tumbling one after the other". ...
随机推荐
- javascript学习之通过class获取元素
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Java并发编程底层实现原理 - volatile
Java语言规范第三版中对volatile的定义如下: Java编程语言允许线程访问共享变量,为了确保共享变量能被准确和一致性的更新,线程应该确保通过排他锁 单独获得这个变量. volatile有时候 ...
- 遍历ArrayList时同时修改引发的问题
看见一篇博客,没有写完整,于是增补了一下: 博客原文:http://www.cnblogs.com/alipayhutu/archive/2012/08/11/2634073.html 注:黄色字体为 ...
- IOS网络第二天 - 06-POST请求
************POST请求 #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @in ...
- Onethink1.1 钩子和插件的使用!
Onethink下载请自行百度咯,安装也就几秒钟. 高手(略),只是针对和我一样需要了解的菜鸟. 主要讲一讲onethink插件的使用,因为这对我们的快速开发有帮助,所以记录一下,同时也希望能够帮助一 ...
- 微博mid和id转换
mid为62进制编码,id为常见的10进制编码. id从低位到高位,7个数字为一组,转换为62进制,并顺序合并,即转换为mid. mid从地位到高位,4个字母为一组,转换为10进制,并右移7位,计算和 ...
- How to install starDIct on suse OS?
1. Access page http://code.google.com/p/stardict-3/ to download starDict package or use zypper in to ...
- JSON语法五大要素图文介绍
原文:http://www.jb51.net/article/32398.htm JSON语法是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成,下面就进行学习研究,希望本文能教会 ...
- Sqlserver 循环表
CREATE TABLE dbo.[User] ( UID BIGINT IDENTITY ,Name ) NOT NULL ,Pwd ) NOT NULL ,CONSTRAINT PK_User P ...
- java://Comparator、Comparable的用法(按照要求将map集合的键值对进行顺序输出)
import java.util.*; public class Person implements Comparable<Person>//使Person的属性具有比较性 { priva ...