博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp
阅读量:6267 次
发布时间:2019-06-22

本文共 3324 字,大约阅读时间需要 11 分钟。

D. New Year and Ancient Prophecy

题目连接:

Description

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 Input

6

123434

Sample Output

8

Hint

题意:

给你一个全是数字的字符串(长度5000),问你多少种划分方案,就可以使得这个字符串分割成了一个绝对递增序列。

题解

DP,dp[i][j]表示以i位置结尾,长度为j的字符串的方案数。转移很简单,就dp[i][j]+=dp[i-j][k](k从1到j-1),如果str[i-j+1][i]>str[i-j-j+1][i-j]的话,dp[i][j]+=dp[i-j][j]。

很显然,dp是n^3的,我们就可以用奇怪的手法去优化一下就好了,我是无脑后缀数组预处理优化的。

代码

#include
using namespace std;long long dp[5005][5005];char str[5005];const int mod = 1e9+7;char s[5005];struct Bit{ int lowbit(int x) { return x&(-x); } long long val[5005]; int sz; void init(int sz){ this->sz=sz; for(int i = 0 ; i <= sz ; ++ i) val[i] = 0 ; } void updata(int pos ,long long key) { while(pos<=sz){ val[pos]+=key; if(val[pos]>=mod) val[pos]-=mod; pos+=lowbit(pos); } } long long query(int pos) { long long res=0; while(pos>0) { res+=val[pos]; if(res>=mod)res-=mod; pos-=lowbit(pos); } return res; }}bit[5005];#define maxn 5005const int inf=0x3f3f3f3f;int wa[maxn],wb[maxn],wn[maxn],wv[maxn];int rk[maxn],height[maxn],sa[maxn],r[maxn],Min[maxn][20],ok[maxn][maxn],n;int cmp(int *r,int a,int b,int l){ return (r[a]==r[b])&&(r[a+l]==r[b+l]);}void da(int *r,int *sa,int n,int m){ int i,j,p,*x=wa,*y=wb,*t; for(i=0;i
=0;i--) sa[--wn[x[i]]]=i; for(j=1,p=1;p
=j) y[p++]=sa[i]-j; for(i=0;i
=0;i--) sa[--wn[wv[i]]]=y[i]; for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i
r) swap(l,r); l++; if(l>r) return n-a; int tmp=int(log(r-l+1)/log(2)); return min(Min[l][tmp],Min[r-(1<
=i+(j-i+1)/2||str[i+tmp]>=str[i+(j-i+1)/2+tmp]) ok[i][j]=0;else ok[i][j]=1; } for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { if(s[i-j+1] == '0') continue; dp[i][j] = 0 ; if(i-j == 0) dp[i][j] ++ ; dp[i][j] += bit[i-j].query(j - 1); if(i-j!=0&&(i-j-j+1)>0){ if(ok[i-j-j][i-1]) dp[i][j] += bit[i-j].query(j)-bit[i-j].query(j-1); } if(dp[i][j]>=mod)dp[i][j]%=mod; bit[i].updata(j,dp[i][j]); } } cout<
<

转载地址:http://sudpa.baihongyu.com/

你可能感兴趣的文章
【阿里云 MVP Tech Show】全面解析 IoT 平台
查看>>
马云云栖大会谈未来三十年 五大变革会颠覆各行各业
查看>>
向服务型制造商转型 CRM功不可没
查看>>
pytesseract OCR 识别
查看>>
杨强教授PPT通俗易懂解密:如何在人工智能浪潮中少走弯路
查看>>
「镁客·请讲」速感科技陈震:以机器视觉为核心,让低成本、高性价比成为机器人行业关键词...
查看>>
「镁客·请讲」潜行科技杨洋:飞行无人机市场膨胀,水下无人机迎来元年
查看>>
sqlserver2008无法连接到local解决方案
查看>>
VR与游戏完美结合?斯皮尔伯格导演的《玩家一号》发布预告片
查看>>
5月15日云栖精选夜读丨阿里巴巴与三个知名车企达成合作,将为其“AI+车”解决方案...
查看>>
想要自定义专属无人机吗?MIT研究的这套系统帮助你
查看>>
量子技术将如何颠覆未来战争形态
查看>>
蚂蚁金服:消息队列事务型消息原理浅析
查看>>
我们为什么需要量子计算
查看>>
Word文档转Markdown插件(Windows)
查看>>
JS 之正则表达式
查看>>
PXC 5.7 mysqldump: Error 2013
查看>>
HID Global荣膺 RFID行业最有影响力国际品牌奖
查看>>
上海市政府颁布智能汽车牌照,蔚来汽车成首批获此资格企业
查看>>
如何让机器理解汉字一笔一画的奥秘?
查看>>