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". ...
随机推荐
- SQL SERVER 数据库操作脚本
创建数据库 create Database MYDB on ( Name=mydb_dat, FileName='c:\data\mydate.mdf',size=10,maxsize=50 ) LO ...
- hihoCoder 1184 连通性二·边的双连通分量
#1184 : 连通性二·边的双连通分量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在基本的网络搭建完成后,学校为了方便管理还需要对所有的服务器进行编组,网络所的老 ...
- 本周psp
本周PSP 类别 内容 开始时间 中止时间 终止时间 总用时 产品计划会议 定义产品的用户需求,以及从这个产品中得到什么.解决啥问题 18:00 0 20:00 120分钟 撰写博客 会议记录与个 ...
- ecmall源码中的 function _config_view()-关于重写
ecmall中类库的继承过程: defaultAPP->MallbaseApp->FrontendApp->EcBaseApp->BaseApp->Object 在bas ...
- 总结-php
strtr('li.a-o_lo.n_g-jun', '-_.', '+/=') 好高级啊 在tomcat里使用php用quercus PHP in java http://quercus.cau ...
- track message forwards, avoiding request loops, and identifying the protocol capabilities of all senders along the request/response chain
https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html The TRACE method is used to invoke a remote, ...
- 论文笔记(1)——《Where's Wally?Precise User Discovery Attacks in Location Proximity Services》
Abstract: 位置相近服务在社交和移动网络的广泛使用是基于可用性和用户隐私的平衡,但引发了三角定位攻击的风险.文章系统化地讨论了此类攻击的防范,包括问题在不同临近模型下的形式化,针对不同模型的有 ...
- js == 与 === 的区别,‘’与“”的区别
js == 与 === 的区别 1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较"转化成同一类型后的值"看"值&quo ...
- Mac mySql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的解决办法
我的环境:Mac 10.11.6 ,mysql 5.7.14 . mac mySql 报错ERROR 2002 (HY000): Can't connect to local MySQL serv ...
- C++ 函数形参发生动态绑定时指针增长步长与静态类型一致
牛客网上的题: class A { public: long a; }; class B : public A { public: long b; }; void seta1(A* data, int ...