Good Bye 2015 D. New Year and Ancient Prophecy

时间:2022-12-29 21:49:59
D. New Year and Ancient Prophecy
time limit per test

2.5 seconds

memory limit per test

512 megabytes

input

standard input

output

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.

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"

一个长为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的更多相关文章

  1. Codeforces Good Bye 2015 D&period; New Year and Ancient Prophecy 后缀数组 树状数组 dp

    D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...

  2. 【27&period;34&percnt;】【codeforces 611D】New Year and Ancient Prophecy

    time limit per test2.5 seconds memory limit per test512 megabytes inputstandard input outputstandard ...

  3. Good Bye 2015 B&period; 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 ...

  4. Good Bye 2015 A&period; New Year and Days 签到

    A. New Year and Days   Today is Wednesday, the third day of the week. What's more interesting is tha ...

  5. Codeforces Good bye 2015 B&period; New Year and Old Property dfs 数位DP

    B. New Year and Old Property 题目连接: http://www.codeforces.com/contest/611/problem/B Description The y ...

  6. Codeforces Good Bye 2015 A&period; New Year and Days 水题

    A. New Year and Days 题目连接: http://www.codeforces.com/contest/611/problem/A Description Today is Wedn ...

  7. Good Bye 2015 B&period; New Year and Old Property —— dfs 数学

    题目链接:http://codeforces.com/problemset/problem/611/B B. New Year and Old Property time limit per test ...

  8. codeforces Good Bye 2015 B&period; New Year and Old Property

    题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...

  9. Good Bye 2015 C&period; New Year and Domino 二维前缀

    C. New Year and Domino   They say "years are like dominoes, tumbling one after the other". ...

随机推荐

  1. kafka源码分析之二客户端分析

    客户端由两种:生产者和消费者 1. 生产者 先看一下生产者的构造方法: private KafkaProducer(ProducerConfig config, Serializer<K> ...

  2. hibernate 实现多表连接查询(转载)

    http://www.cnblogs.com/lihuiyy/archive/2013/03/28/2987531.html 为了方便,直接粘过来,方便查看.不收藏了 Hibernate主要支持两种查 ...

  3. 流媒体基础实践之——RTMP直播推流

    一.RTMP推流:用户可将RTMP视频流推送到阿麦提供的打流地址.地址格式类似于: rtmp://livepush.myqcloud.com/live 现在可以支持哪些直播源?和那些直播软件?推流参数 ...

  4. 高性能javascript及页面注意事项

    1.少用全局变量 原因:因为作用域链是一个堆栈的结构,所以遵循先进先出的原则,而javascript引擎在解析代码的时候,将全局对象放在栈底,然后向上依次出现的是不同作用域的活动对象(这些活动对象除了 ...

  5. mysql主从复制-linux版本

    来自:http://www.osyunwei.com/archives/7269.html,改版 mysql主从复制本文采用的是centos6.5+mysql-5.6.23版本之前在 windows7 ...

  6. 实现在Android 多点手势识别

    google 提供的API中,有个类,大家都很熟悉,GestureDetector.使用它,我们可以识别用户通常会用的手势.但是,这个类不支持多点触摸(可能 google认为没有人会在几个手指都在屏幕 ...

  7. java调用C&plus;&plus; DLL库方法

    最近一个项目要开发网页端人脸识别项目,人脸识别的算法已经写好,是C++版,但是网页端要求使用Java后台,这就涉及到Java调用DLL的问题.经过查找,实现了一个简单的例子. 1.第一步,先在Java ...

  8. tmux手册中文翻译

    man tmux可以看到最详细的tmux介绍,本文翻译自tmux手册. tmux全名叫"terminal multiplexer",终端多路复用器. tmux的命令格式为: tmu ...

  9. 如何判断一个文本文件内容的编码格式 UTF-8 &quest; ANSI&lpar;GBK&rpar;

    转自:http://blog.csdn.net/jiangqin115/article/details/42684017 UTF-8编码的文本文档,有的带有BOM (Byte Order Mark, ...

  10. PHP中关于foreach的笔试题

    1,php与C++的不同之处是PHP中变量没有局部作用域,只有函数作用域和全局作用域.如下函数,在php中,$name的作用域是函数test():在C++中$name的作用域是for循环体,for循环 ...