【Goodbye2015】Codeforces 611D New Year and Ancient Prophecy【Dp+前缀和优化+预处理字符串】好题!好题!
来源:互联网 发布:虚无世界2java路径 编辑:程序博客网 时间:2024/06/05 21:10
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.
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'.
Print the number of ways to correctly split the given sequence modulo 109 + 7.
6123434
8
820152016
4
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的数字串,让你将其分割成若干个子串(不要求长度和个数),要求分割出来的子串要按照相对顺序严格递增排列才行。
问你一共有多少种可行方案。
思路:
统计可行方案数,再考虑前边的分割点会影响后边分割点的选取,肯定是dp模型无疑了。
那么设定dp【i】【pre】,表示以【pre~i】作为最后一个子串出现的方案数。
那么我们不难推出其状态转移方程:
dp【i】【pre】+=dp【pre-1】【pre-1-z】(0<=z<=i-pre);
①当i-pre>z>=0的时候,明显后边的子串是一定大于前边的子串的(根据长度判断);
那么一定有:dp【i】【pre】+=dp【pre-1】【pre-1-z】(0<=z<i-pre);
②那么当z==i-pre的时候。就是相邻两个子串长度相等的时候,这里我们需要判断两个子串的大小,然后进行转移。
③显然暴力直接搞是要超时的,那么考虑可以将①部分维护一个dp前缀和,那么就有:dp【i】【pre】+=sum【pre-1】【pre-1】-sum【pre-1】【pre-1-(i-pre)】;
④优化到这里是远远不够的,如果我们暴力去判断相等长度时候两个子串的大小,依然会超时,那么考虑优化比较。
我们优化比较时间,很显然要优化掉不需要的操作,显然我们希望优化掉的不需要的操作,就是两个子串的最长公共前缀部分。
假设我们两个子串分别是:【l,r】,【l2,r2】;那么我们只要知道了两个子串的最长公共前缀的长度Len.那么此时只要判断a【l+Len】和a【l2+Len】的大小即可。
⑤那么我们考虑维护这个最长公共前缀,设定cnt【l】【l2】表示两个子串的起点分别是l,l2的最长公共前缀的长度。那么对于这一部分,我们只要逆序枚举位子然后维护一波即可。具体参考代码吧,口述说的越多越乱。
⑥那么做到了上述部分,时间复杂度就彻底的降到了O(n^2),剩下的注意取模和动态维护各个数组就没别的了;
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define mod 1000000007#define ll __int64char a[5055];int cnt[5055][5055];ll dp[5005][5005];ll sum[5005][5005];int judge(int l,int r,int l2,int r2){ int i=l;int j=l2; while(i<=r) { if(a[i]>a[j])return 1; else if(a[i]==a[j])i++,j++; else return 0; } return 0;}int main(){ int n; while(~scanf("%d",&n)) { memset(dp,0,sizeof(dp)); scanf("%s",a+1); dp[0][0]=1; sum[0][0]=1; for(int i=n;i>0;--i) { for(int j=n;j>i;--j) { if(a[i]!=a[j]) cnt[i][j]=0; else cnt[i][j]=cnt[i+1][j+1]+1; } } for(int i=1;i<=n;i++) { for(int pre=1;pre<=i;pre++) { if(a[pre]=='0')continue; if(pre-1>=0) dp[i][pre]=(dp[i][pre]+sum[pre-1][pre-1])%mod; if(pre-1-(i-pre)>=0) dp[i][pre]=(dp[i][pre]-sum[pre-1][pre-1-(i-pre)]+mod)%mod; int l=pre-1-(i-pre); int r=pre; if(pre-1>=0&&pre-1-(i-pre)>=0&&a[l+cnt[l][r]]<a[r+cnt[l][r]]&&l+cnt[l][r]<r) { if(a[pre-1-(i-pre)]=='0')continue; dp[i][pre]=(dp[i][pre]+dp[pre-1][pre-1-(i-pre)])%mod; } } for(int pre=1;pre<=i;pre++) { sum[i][pre]=sum[i][pre-1]+dp[i][pre]; dp[i][pre]%=mod; } } ll output=0; for(int i=n;i>=1;i--) { output+=dp[n][i]; output%=mod; } printf("%I64d\n",(output+mod)%mod); }}
- 【Goodbye2015】Codeforces 611D New Year and Ancient Prophecy【Dp+前缀和优化+预处理字符串】好题!好题!
- codeforces-611D-New Year and Ancient Prophecy【lcp+dp】【好题】
- codeforces-Goodbye2015- New Year and Ancient Prophecy
- codeforces GoodBye2015 D.New Year and Ancient Prophecy (dp+lcp+树状数组)
- CodeForces 611 D. New Year and Ancient Prophecy(dp)
- Codeforces 611D New Year and Ancient Prophecy DP
- Codeforces 611D New Year And Ancient Prophecy DP+递推式优化
- Codeforces 611D New Year and Ancient Prophecy (Good Bye 2015 D题)
- CodeForces 611D New Year and Ancient Prophecy
- 【27.34%】【codeforces 611D】New Year and Ancient Prophecy
- DP - CF 611D New Year and Ancient Prophecy
- Codeforces 611D:New Year and Ancient Prophecy DP 分块记录最后一个
- 【Codeforces Good Bye 2015】D. New Year and Ancient Prophecy
- codeforces Good Bye 2015 D. New Year and Ancient Prophecy
- Codeforces Good Bye 2015 D. New Year and Ancient Prophecy DP
- Good Bye 2015 D. New Year and Ancient Prophecy(dp+LCP)
- Good Bye 2015 D. New Year and Ancient Prophecy(dp+lcp)
- CF Good Bye 2015 C. New Year and Domino && D. New Year and Ancient Prophecy (DP)
- Form表单ajax提交数据不跳转
- Accelerated C++学习笔记1 unit0
- superset开发环境搭建
- ccf-压缩编码
- Linux Kernel设备驱动模型之 struct device_type
- 【Goodbye2015】Codeforces 611D New Year and Ancient Prophecy【Dp+前缀和优化+预处理字符串】好题!好题!
- 每一个程序员都应该知道的高并发处理技巧、创业公司如何解决高并发问题、互联网高并发问题解决思路、caoz大神多年经验总结分享
- Python笔记之字典
- 高通如何优化FFBM启动时间
- cookie实现单点登录
- iOS开发中读取csv文件
- mysql的分区技术详细介绍
- 机器学习模型应用以及模型优化的一些思路
- Gson 转换工具类