http://codeforces.com/gym/100712/attachments

题意:

给出一个01串,现在要切割这个01串,使得每个子串长度都不大于k,并且每个子串不能01交替出现,单个字符是合法的。

思路:
很容易想到用dp去做,但是怎么做呢?

我们可以先预处理一下i~j是否是合法的子串,即是否是01交替出现的串。

接下来我们从尾部开始,d【i】表示i~n所需的最少切割数。那么每次在左边新加入一个数字时,怎么确定它的最少切割数呢?

设f【i】【t】不是01交替的串并且长度是小于等于k的,那么在t点是可以切割的,我们只需要枚举切割点找最小值就可以了。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,long long> pll;
const int INF = 0x3f3f;
const int maxn=+; int n,k;
char str[maxn]; int f[maxn][maxn];
int d[maxn]; int main()
{
//freopen("input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
getchar();
scanf("%s",str+); memset(f,,sizeof(f)); for(int i=;i<=n;i++)
{
f[i][i]=;
for(int j=i+;j<=n;j++)
{
if(f[i][j-] && str[j]!=str[j-]) f[i][j]=;
else break;
}
f[i][i]=;
} memset(d,INF,sizeof(d));
d[n+]=-; for(int i=n;i>=;i--)
{
for(int j=i;j<=i+k-&&j<=n;j++)
{
if(i==j||!f[i][j])
d[i]=min(d[i],d[j+]+);
}
} printf("%d\n",d[]);
}
return ;
}

Gym - 100712D Alternating Strings的更多相关文章

  1. Gym 100712L Alternating Strings II(单调队列)

    题目链接 Alternating Strings II 题意是指给出一个长度为n的01串,和一个整数k,要求将这个01串划分为很多子串(切很多刀),使得每个子串长度不超过k,且每个字串不是01交替出现 ...

  2. Alternating Strings Gym - 100712D 简单dp && Alternating Strings II Gym - 100712L 数据结构优化dp

    比赛链接:https://vjudge.net/contest/405905#problem/D 题意: 给你一个长度为n的由0或1构成的串s,你需要切割这个串,要求切割之后的每一个子串长度要小于等于 ...

  3. codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...

  4. Gym 100247B Similar Strings(哈希+思维)

    https://vjudge.net/problem/Gym-100247B 题意: 如果两个字符串通过映射后是一样的,则说明这两个字符串是相似的,现在给出n个字符串,计算出有多少组字符串是相似的. ...

  5. 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest

    Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...

  6. 2015 ACM Amman Collegiate Programming Contest 题解

    [题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...

  7. 【TP SRM 703 div2 250】AlternatingString

    Problem Statement A string of zeros and ones is called an alternating string if no two adjacent char ...

  8. Binary Strings Gym - 101161G 矩阵快速幂 + 打表

    http://codeforces.com/gym/101161/attachments 这题通过打表,可以知道长度是i的时候的合法方案数. 然后得到f[1] = 2, f[2] = 3, f[3] ...

  9. ACM: Gym 100935B Weird Cryptography - 简单的字符串处理

    Weird Cryptography Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. bond的操作方式

    本文转自网上,版权归属原作者,原文地址 :https://www.cnblogs.com/5201351/p/4898342.html 操作系统:CentOS Linux release 7.1.15 ...

  2. centos7上开启单用户模式

    author: headsen  chen date : 2018-08-02  11:23:16 1,在出现centos7的页面的时候,按 e进入到编译内核的界面 2,找到下面的这一段,将ro 该车 ...

  3. windowSoftInputMode

    有个问题困扰我一晚上,每次进入Activity后,EditText自动获得焦点弹出软键盘,键盘遮挡listView,使得无法显示最后一条消息.我在edittext点击事件中也设定了,listView. ...

  4. oracle的START WITH CONNECT BY PRIOR用法

    转自:https://www.cnblogs.com/linjiqin/archive/2013/06/24/3152674.html Oracle 树操作(select…start with…con ...

  5. SVN 配置和使用

    SVN使用环境 使用SVN管理源代码,必须有2套环境 服务器 用来存储客户端上传的源码 一般都是在Windows环境下安装Visual SVN Server 客户端 用来提交.回退.修改.下载等操作 ...

  6. yii2设置发送邮件的一些配置

    错误提示: Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto in C:\xampp\ht ...

  7. Oracle等待事件之db file sequential read/ db file parallel read

    1.产生原因 db file sequential read这个是非常常见的I/O 相关的等待事件.表示发生了与索引扫描相关的等待.意味着I/O 出现了问题,通常表示I/O竞争或者I/O 需求太多. ...

  8. 利用阿里云腾讯云正版KMS服务器端口转发

    注意:以下内容仅供实验,请勿用于任何非法用途我们知道,阿里云和腾讯云在内网部署了KMS服务器,而且是正版的,那么,有没有办法使用公网的计算机直接或间接连接到这些KMS服务器呢,受代理服务器和跳板机配置 ...

  9. talib 中文文档(五):文档导航

    Documentation 安装和问题 快速使用 高级应用 方法分类 Overlap Studies 重叠的研究 Momentum Indicators 动量指标 Volume Indicators ...

  10. importlib应用 - django

    背景 仿django的中间件的编程思想 用户可通过配置,选择是否启用某个组件/某个功能,只需要配置 eg:报警系统,发邮件,发微信 ... ( 根据字符串导入模块, 利用反射找到模块下的类,实例化.执 ...