Good Bye 2015 D. New Year and Ancient Prophecy

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. 哈希

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <vector>
 7 #include <ctime>
 8 #define oo 1000000007
 9 #define maxn 5200
10 #define maxm 4200
11 
12 using namespace std;
13 
14 int n;
15 string s;
16 int f[maxn][maxn];
17 long long hash[maxn][maxn];
18 //f[x][y] 表示以x结尾的长度小于等于y的答案
19 
20 bool judge(int x,int y,int len)
21 {
22     int l=0,r=len-1,d=0;
23     while (l<=r)
24     {
25         int mid=(l+r)>>1;
26         if (hash[x][x+mid]!=hash[y][y+mid])
27         {
28             r=mid-1;
29             d=mid;
30         }
31         else
32             l=mid+1;
33     }
34     return s[x+d-1]<s[y+d-1];
35 }
36 
37 int main()
38 {
39     //freopen("D.in","r",stdin);
40     scanf("%d",&n);
41     cin>>s;
42     memset(hash,0,sizeof(hash));
43     for (int i=1;i<=n;i++)
44         for (int j=i;j<=n;j++)
45             hash[i][j]=hash[i][j-1]*131+s[j-1];
46     f[0][0]=1;
47     for (int i=1;i<=n;i++)
48     {
49         for (int j=1;j<=i;j++)
50         {
51             if (s[i-j]=='0')
52             {
53                 f[i][j]=f[i][j-1];
54                 continue;
55             }
56             int p;
57             //p是前一段可以取得最长长度
58             if (i-2*j>=0)
59             {
60                 if (judge(i-2*j+1,i-j+1,j)) p=j;
61                 else p=j-1;
62             }
63             else
64                 p=i-j;
65             f[i][j]=(f[i][j-1]+f[i-j][p])%oo;
66         }
67     }    
68     printf("%d\n",f[n][n]);
69     return 0;
70 }
hash

2.dp

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <vector>
 7 #include <ctime>
 8 #define oo 1000000007
 9 #define maxn 5200
10 #define maxm 4200
11 
12 using namespace std;
13 
14 int n;
15 string s;
16 int f[maxn][maxn],low[maxn][maxn];
17 //f[x][y] 表示以x结尾的长度小于等于y的答案
18 //low[x][y] 表示从x、y开始的最小的不相等的偏移量
19 
20 bool judge(int x, int y, int len)
21 {
22     int d=low[x][y];
23     return d<len && s[x+d-1]<s[y+d-1];
24 }
25 
26 int main()
27 {
28     //freopen("D.in","r",stdin);
29     scanf("%d",&n);
30     cin>>s;
31     for (int i=n;i>=1;i--)
32         for (int j=n;j>i;j--)
33             if (s[i-1]!=s[j-1])
34                 low[i][j]=0;
35             else
36                 low[i][j]=low[i+1][j+1]+1;
37     f[0][0]=1;
38     for (int i=1;i<=n;i++)
39         for (int j=1;j<=i;j++)
40         {
41             if (s[i-j]=='0')
42             {
43                 f[i][j]=f[i][j-1];
44                 continue;
45             }
46             int p;
47             //p是前一段可以取得最长长度
48             if (i-2*j>=0)
49             {
50                 if (judge(i-2*j+1,i-j+1,j)) p=j;
51                 else p=j-1;
52             }
53             else
54                 p=i-j;
55             f[i][j]=(f[i][j-1]+f[i-j][p])%oo;
56         }
57     printf("%d\n",f[n][n]);
58     return 0;
59 }
dp

 

转载于:https://www.cnblogs.com/zig-zag/p/5093339.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值