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

最新文章

  1. awk命令速查
  2. jstl 小总结 以及 jstl fn
  3. Papa Parse – 超强大的多线程 CSV 文本解析库
  4. [转]asp.net解决高并发的方案.
  5. STM32的PWM输出极性的问题
  6. struts2 json关于Date日期的解析
  7. 8.3-8.7 usaco
  8. Mondriaan&#39;s Dream
  9. PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: &#39;C:\\Users\\video\\AppData\\Local\\Temp\\tmpfipzk8ma&#39;--问题解决
  10. Samsung K9F1G08U0D SLC NAND FLASH简介(待整理)
  11. php5.6解决curl扩展不生效的问题
  12. javascript 中 undefined 和 null 区别
  13. RH033读书笔记(4)-Lab 5 File Permissions
  14. 认识&lt;meta&gt;
  15. IOS开发-OC学习-常用功能代码片段整理
  16. 【新手向】自用的tooltip小插件,前端插件知识科普~
  17. 【spring实战第五版遇到的坑】4.2.3中LDAP内嵌服务器不启动的问题
  18. mac电脑上不能用移动硬盘的原因和方法
  19. 遍历删除查临时表相关session再操作表
  20. Java小白不走弯路学习Java流程以及学习误区

热门文章

  1. 10 款最好的 Python IDE
  2. 【Git学习笔记】远程仓库
  3. ssh 使用密钥与登录进行远程cp
  4. 更改ubuntu多系统启动顺序
  5. ztree已拥有权限显示
  6. LeetCode Paint House II
  7. iOS中的__typeof与typeof
  8. Linux CentOS 中安装 MySql
  9. Web自动化测试工具调研
  10. MVC中权限管理