Codeforces Round #396(Div. 2) C. Mahmoud and a Message (dp)
来源:互联网 发布:数据对比分析的方法 编辑:程序博客网 时间:2024/06/17 07:46
思路:
首先定义dp数组,dp[i]代表长度为i的字符串最多的种类数。
对于已经存在的长度为l的字符串,我们从下一个字符k,即dp[k+1]开始转移状态。
对于这个状态我们可以试着由 dp[k]和dp[k+1]转移过来(如果limit满足条件的话,然而从题目可知,长度为1的一定会满足条件),也可以试着由dp[k-1]和字符串(k,k+1)转移过来(如果满足limit)。
当然limit是随着每个向前的字符会更新的越来越小的。
所以当不满足的时候,break。
对于第二问,问你最长的一个是多长,那么在从i向前走的过程中记录一下i-j+1的长度就好。
对于第三问,问你最少分成几个串,那么我们额外开一个数组ans3,初始化ans3[0] = 1
然后在每次更新种类数的时候,ans3[i] = min(ans[i],ans[j-1]+1);
#include <string.h>#include <iostream>#include <cstdio>#include <queue>#include<algorithm>#include <map>using namespace std;typedef long long int lli;#define inf 0x3f3f3f3fconst int mod = 1e9+7;char s[1200];int cnt[30];lli dp[1100];//统计种类数int ans3[1100];//统计长度int main(){ int len,ans2 = 1,lim = 2000000; memset(ans3,0x3f,sizeof(ans3)); scanf("%d",&len); scanf("%s",s); for(int i = 1;i <= 26;i++){ scanf("%d",&cnt[i]); } dp[0] = 1;ans3[0] = 1; for(int i = 1;i < len;i++){ lim = cnt[ s[i]-'a'+1 ]; for(int j = i;j > i-cnt[ s[i]-'a'+1 ];j--){ if(j < 0) break; lim = min(lim,cnt[s[j]-'a'+1]); if(i-j+1 <= lim){ if(j != 0) dp[i] = (dp[i]+dp[j-1]) % mod; else// 跑到最左边了。情况加1 dp[i] = (dp[i]+1) % mod; ans2 = max(ans2,i-j+1); ans3[i] = min(ans3[i],ans3[j-1]+1); } else{ break; } } } printf("%lld\n%d\n%d\n",dp[len-1],ans2,ans3[len-1]);}
阅读全文
0 0
- Codeforces Round #396 (Div. 2) C:Mahmoud and a Message(dp)
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message (新手DP)
- Codeforces Round #396(Div. 2) C. Mahmoud and a Message (dp)
- Codeforces Round #396 (Div. 2)C. Mahmoud and a Message
- Codeforces Round #396 (Div. 2) C.Mahmoud and a Message
- Codeforces Round #396 (Div. 2) C.Mahmoud and a Message
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message
- Codeforces Round #396 (Div. 2)-C. Mahmoud and a Message(dp)
- Codeforces Round #396(Div. 2)C. Mahmoud and a Message【dp】
- C.Mahmoud and a Message Codeforces Round #396 (Div. 2)(DP)
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message 倒着DP
- Codeforces Round #396 (Div. 2)Mahmoud and a Message
- Codeforces Round #396 (Div. 2) - Problem C - Mahmoud and a Message
- Codeforces 766 C Mahmoud and a Message 详解(DP)
- Codeforces Round #396 (Div. 2)Mahmoud and a Dictionary
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip(树形dp+二进制按位拆分)
- Codeforces 766C Mahmoud and a Message 基础DP
- codeforces 766 c Mahmoud and a Message(dp)
- Python3 数据结构
- Wooden Sticks
- hihoCoder 1430 A Boring Problem(数论)
- mysql 安装
- Sequence_Stack(顺序栈的实现)
- Codeforces Round #396(Div. 2) C. Mahmoud and a Message (dp)
- LinuxStudyNote(41)-RPM包的依赖性、树形依赖、环形依赖解决方法、模块依赖
- Ubuntu 14.04 FTP服务器--vsftpd的安装和配置
- 14调整数组顺序使奇数位于偶数前面python
- gtk图形界面编程day03
- 14.leetCode500:keyboard row
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B. Train Seats Reservation
- ASP.NET Razor
- eclipse创建maven版的web工程