C. Mahmoud and a Message dp + 暴力
http://codeforces.com/contest/766/problem/C
关键在于dp,如何计数。
设dp[i]表示前i个字母中,能分成多少份合法的情况。那么答案就是dp[n],其中dp[0] = 1;
比如说:aab的,也就是样例。
对于每一个i,枚举其往后能组合成那个,比如b,能组合成b,也就是自己一个,或者ab,但是不能aab,因为违反了条件。
那么,组合成b的时候,就应该加上dp[2]的值,也就是前2个分成的合法情况有多少种,b作为单独一个插去后面。
ab同理。
dp[1] = 1,也就是只有a,
dp[2] = 2,有a/a 和 aa
dp[3] = 3,有a/a/b、aa/b和a/ab
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset> const int MOD = 1e9 + ;
const int maxn = 1e3 + ;
int cnt[maxn][ + ];
char str[maxn];
int a[maxn];
int dp[maxn];
bool check(int last, int pre) {
for (int i = ; i <= ; ++i) {
if (cnt[last][i] - cnt[pre - ][i] != && last - pre + > a[i]) return false;
}
return true;
}
void work() {
int n;
cin >> n;
cin >> str + ;
for (int i = ; i <= ; ++i) cin >> a[i];
for (int i = ; i <= n; ++i) {
for (int j = ; j <= ; ++j) {
if (str[i] - 'a' + == j) {
cnt[i][j] = cnt[i - ][j] + ;
} else cnt[i][j] = cnt[i - ][j];
}
}
int ansone = , anstwo = -inf;
for (int i = ; i <= n; ++i) {
for (int j = i; j >= ; --j) {
if (check(i, j)) {
anstwo = max(anstwo, i - j + );
} else break;
}
}
int ansthree = ;
int pre = ;
for (int i = ; i <= n; ++i) {
if (check(i, pre) == false) {
ansthree++;
pre = i;
}
}
dp[] = ;
for (int i = ; i <= n; ++i) {
for (int j = i; j >= ; --j) {
if (!check(i, j)) break;
dp[i] += dp[j - ];
dp[i] %= MOD;
}
}
// for (int i = 1; i <= n; ++i) {
// cout << dp[i] << endl;
// ansone += dp[i];
// ansone %= MOD;
// }
// ansone = (ansone + MOD - n + 1) % MOD;
cout << dp[n] << endl;
cout << anstwo << endl;
cout << ansthree << endl;
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS;
work();
return ;
}
C. Mahmoud and a Message dp + 暴力的更多相关文章
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message dp
C. Mahmoud and a Message 题目连接: http://codeforces.com/contest/766/problem/C Description Mahmoud wrote ...
- Codeforces 766C - Mahmoud and a Message
C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 766C Mahmoud and a Message 2017-02-21 13:57 62人阅读 评论(0) 收藏
C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message
地址:http://codeforces.com/contest/766/problem/C 题目: C. Mahmoud and a Message time limit per test 2 se ...
- 【codeforces 766C】Mahmoud and a Message
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 766C:Mahmoud and a Message(DP)
题目链接:http://codeforces.com/problemset/problem/766/C 题意 有一个长度为n的字符串,第二行有26个数字,位置1~26对应为a~z的字母,数值表示该字母 ...
- codeforces 766 C. Mahmoud and a Message(简单dp)
题目链接:http://codeforces.com/contest/766/problem/C 题意:给你一个长度为n的字符串,这个字符串只包含小写字母,然后让你把这个字符串进行分割,形成若干个小的 ...
- POJ 2029 DP || 暴力
在大矩形中找一个小矩形 使小矩形包括的*最多 暴力或者DP 水题 暴力: #include "stdio.h" #include "string.h" int ...
- 区间Dp 暴力枚举+动态规划 Hdu1081
F - 最大子矩形 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
随机推荐
- SqlServer 经常使用分页方法总结
SqlServer 经常使用分页方法总结 以下演示样例总结了,SqlServer数据库 经常使用分页方法,仅供学习參考 A. 使用 RowNumber 和 Between And 组合分页: /*** ...
- wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合
wpf 导出Excel 1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...
- HDU 1176-免费馅饼(DP_逆推)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 机器学习笔记之PCA-SIFT总结
不多说,直接上干货! PCA-SIFT算法在描述子构建上作了创新,主要是 将统计学中的主成分分析(PCA)应用于对描述子向量的降维,以提高匹配效率 . PCA 的原理是:一般有效信号的方差大,噪声的方 ...
- 3 TypeScript 语法特性
一.类型注解(Type annotations) TypeScript 通过类型注解提供静态类型以在编译时启动类型检查,简单来说,就是指定数据类型,它会在代码运行的时候,对传入的数据进行数据类型匹配检 ...
- 51NOD 1821 最优集合 栈
1821 最优集合 一个集合S的优美值定义为:最大的x,满足对于任意i∈[1,x],都存在一个S的子集S',使得S'中元素之和为i. 给定n个集合,对于每一次询问,指定一个集合S1和一个集合S2, ...
- cygwin安装sshd服务(win7)Error installing a service: OpenSCManager: Win32 error 5:
Error installing a service: OpenSCManager: Win32 error 5: 出现这个问题的解决办法:win7系统管理员运行Cygwin软件 ...
- 怎样快速刪除Word中超链接?
有时我们从网上down了一些资料,存到Word文档里,会发现一些文字和图片带有超链接.这其实是Word自动修改功能引起的麻烦,那么,有什么办法可以把这些超链接快速批量删掉吗? 步骤/方法 1 按键盘上 ...
- res_d_l =[{'contents':d.contents,'href':d.attrs['href']} for d in rd] 泛型
from selenium import webdriverfrom selenium.webdriver.chrome.options import Options# from selenium.w ...
- cgi fastcgi wsgi nginx python Dispatching TurboGears Python via FCGI
https://www.nginx.com/resources/wiki/start/topics/examples/simplepythonfcgi/ 117 2018-06-28 19:56:42 ...