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. hdu4976 A simple greedy problem.

    A simple greedy problem. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  2. github(其他类似github)下载到本地配置

    首先是先装完插件,低版本的myeclipse要自己装插件, 如何使window->show view->other找到git 注意是选第二个... 然后next->next-> ...

  3. CentOS中用户不在 sudoers 文件中。此事将被报告。

    首先切换为root用户 su root; 然后更改etc/sudoers文件的只读为可读可写可执行. sudo chmod 777 /etc/sudoers 输入visudo命令即可编辑文件,找到ro ...

  4. ThinkPHP流程控制!

    IF判断: 在thinkphp 中不能使用 <> 这样的尖括号: <if condition='表达式'> <elseif condition='表达式'/> &l ...

  5. 170808、生成为CVS文件

    /** * Desc : 生成为CVS文件 * User : RICK * @param data 源数据List * @param map csv文件的列表头map * @param outPutP ...

  6. sql 将一张表中的数据插入到另一张表

    将表T_wz_wz中的部分数据插入到表t_wz_kc: insert into t_wz_kc(wzid,jldwid,kcsl,yfpkcsl,cshwcbz) select wzid,jldwid ...

  7. ubuntu16安装使用chrome

    1. sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/ 将下载源加入到系统的源 ...

  8. 用 chown 和 chmod 修改目录所属用户及权限

    1.修改 tmp 目录所属用户为 root,用户组为 root chown -R root:root /tmp12.修改 tmp 目录为可写权限 chmod -R 777 /tmp

  9. 002-Spring Framework-Core-01-IoC容器

    一.概述 文章地址:https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/ 核心主要包括:IoC co ...

  10. Controller中返回数据总结(ResponseEntity,@ResponseBody,@ResponseStatus)

    在传统的开发过程中,我们的控制CONTROLLER层通常需要转向一个JSP视图:但随着WEB2.0相关技术的崛起,我们很多时候只需要返回数据即可,而不是一个JSP页面. ResponseEntity: ...