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. New Year and Ancient Prophecy 后缀数组 树状数组 dp

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

  2. 【27.34%】【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. 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. 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. 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. 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. 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. New Year and Old Property

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

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

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

随机推荐

  1. c++代码中,使用svn版本号作为程序版本号的实现方法

    1.编写版本模板文件 #ifndef _VERSIONSVN_H_#define _VERSIONSVN_H_#define VER_REVISIONSVN $WCREV$#endif //!_VER ...

  2. mock the facade

    mock the facade: Auth::shouldReceive('user')->andReturn($user = m::mock('StdClass')); $user->s ...

  3. dock-compose 安装

    apt-get install python-pip python-dev pip install -U docker-composechmod +x /usr/local/bin/docker-co ...

  4. 准备上线,切换到master分支,报错

    切换到master分支,准备上线,把上次上线sourceTree保存的修改拉出来: 运行,报错了: stackOverflow一搜说要删除旧的: 我show in finder 把他删了,然后双击安装 ...

  5. adv钓鱼题

    4 10 4 5 6 2 10 2 10 4 5 6 2 10 2 60 1 20 29 20 50 20 60 1 20 20 20 60 20 #include<iostream> u ...

  6. JSP数据交互

    JSP数据交互   一.jsp中java小脚本 1.<% java代码段%> 2.<% =java表达式%>不能有分号 3.<%!成员变量和函数声明%>二.注释 1 ...

  7. dragdrop 修改版

    <!DOCTYPE html> <html lang="en"> <head> <meta name="keywords&quo ...

  8. DWR实现后台推送消息到web页面

    DWR简介 DWR(Direct Web Remoting)可用于实现javascript直接调用java函数和后台直接调用页面javascript代码,后者可用作服务端推送消息到Web前端. (服务 ...

  9. GCD的简单用法

    /* 创建一个队列用来执行任务,TA属于系统预定义的并行队列即全局队列,目前系统预定义了四个不同运行优先级的全局队列,我们可以通过dispatch_get_global_queue来获取它们 四种优先 ...

  10. goim socket丢包粘包问题解决。

    -(NSInteger)bytesToInt:(unsigned char*) data { return (data[3]&255)|(data[2]&255)<<8|( ...