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 + 暴力的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 【codeforces 766C】Mahmoud and a Message

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. Codeforces 766C:Mahmoud and a Message(DP)

    题目链接:http://codeforces.com/problemset/problem/766/C 题意 有一个长度为n的字符串,第二行有26个数字,位置1~26对应为a~z的字母,数值表示该字母 ...

  7. codeforces 766 C. Mahmoud and a Message(简单dp)

    题目链接:http://codeforces.com/contest/766/problem/C 题意:给你一个长度为n的字符串,这个字符串只包含小写字母,然后让你把这个字符串进行分割,形成若干个小的 ...

  8. POJ 2029 DP || 暴力

    在大矩形中找一个小矩形 使小矩形包括的*最多 暴力或者DP  水题 暴力: #include "stdio.h" #include "string.h" int ...

  9. 区间Dp 暴力枚举+动态规划 Hdu1081

    F - 最大子矩形 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status Des ...

随机推荐

  1. 剑指Offer面试题43(Java版):n个骰子的点数

    题目:把n个骰子仍在地上.全部骰子朝上一面的点数之和为s,输入n,打印出s的全部可能的值出现的概率. 解法一:基于递归求骰子的点数,时间效率不够高 如今我们考虑怎样统计每个点数出现的次数. 要向求出n ...

  2. 《The Swift Programming Language》的笔记-第27页

    页 1 type safelanguage 本页的主要内容是说swift语言是"类型检查"的安全型编程语言.意思是赋值语句的左值和右值的类型要一致,左值声明是string型变量那么 ...

  3. linux系列之-—01 shell编程笔记

    一.特殊变量($0.$1.$2. $?. $# .$@. $*) shell编程中有一些特殊的变量可以使用.这些变量在脚本中可以作为全局变量来使用. 名称 说明 $0 脚本名称 $1-9 脚本执行时的 ...

  4. eclipse下对中文乱码问题的一些思考

    一.浏览器问题 当你的html页面或jsp页面没有显式声明页面编码的时候,也就是没有下面其中之一的代码 <meta http-equiv="content-type" con ...

  5. android各种菜单使用介绍

    Android菜单的有这几种: 1,OptionMenue:选项菜单 2,contextMenu:上下文菜单 3,SubMenu子菜单 其中,OptionMenue与contextMenu的区别(Op ...

  6. Num 36 : ZOJ 2100 [ 深度优先搜索算法 ] [ 回溯 ]

    该题是用回溯法来解决的题: 题目: Seeding Time Limit: 2 Seconds      Memory Limit: 65536 KB It is spring time and fa ...

  7. C# 中串口通信 serialport1.DataReceived 函数无法触发或者出发延时等等问题解决方法

    以前这个问题困扰我多天最后查资料一大堆,最后最终攻克了,看到非常多人做C#串口都遇到相同的问题,所以写一篇博文,以便学习交流. 一定要在com实例化的时候设置ReceivedBytesThreshol ...

  8. (2)mac下安装MySql数据库软件

    一,软件下载: https://dev.mysql.com/downloads/mysql/ 也可以从其他资源下载.不一定非要官方下载 二,安装 这个比较简单,之间双击开启安装程序,一直下一步就可以. ...

  9. leetcode 400 Add to List 400. Nth Digit

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is ...

  10. 认识虚拟化(virtualization)

    0. 基本定义 虚拟化的本质是将现有的计算机资源通过虚拟化的技术分割成若干个计算机资源,这些计算机资源互相独立:其最终目标是提高计算机的利用效率以最大化计算机的灵活性: 虚拟化为数据.计算能力.存储资 ...