Gym - 100712D Alternating Strings
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的更多相关文章
- Gym 100712L Alternating Strings II(单调队列)
题目链接 Alternating Strings II 题意是指给出一个长度为n的01串,和一个整数k,要求将这个01串划分为很多子串(切很多刀),使得每个子串长度不超过k,且每个字串不是01交替出现 ...
- Alternating Strings Gym - 100712D 简单dp && Alternating Strings II Gym - 100712L 数据结构优化dp
比赛链接:https://vjudge.net/contest/405905#problem/D 题意: 给你一个长度为n的由0或1构成的串s,你需要切割这个串,要求切割之后的每一个子串长度要小于等于 ...
- codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)
题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...
- Gym 100247B Similar Strings(哈希+思维)
https://vjudge.net/problem/Gym-100247B 题意: 如果两个字符串通过映射后是一样的,则说明这两个字符串是相似的,现在给出n个字符串,计算出有多少组字符串是相似的. ...
- 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 ...
- 2015 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...
- 【TP SRM 703 div2 250】AlternatingString
Problem Statement A string of zeros and ones is called an alternating string if no two adjacent char ...
- Binary Strings Gym - 101161G 矩阵快速幂 + 打表
http://codeforces.com/gym/101161/attachments 这题通过打表,可以知道长度是i的时候的合法方案数. 然后得到f[1] = 2, f[2] = 3, f[3] ...
- ACM: Gym 100935B Weird Cryptography - 简单的字符串处理
Weird Cryptography Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- 4.querystring属性
1.querystring.stringify(obj[, sep[, eq[, options]]]) 序列化, 第二个参数分隔符, 第三个参数是对象分隔符 querystring.stringif ...
- 【BZOJ1877】[SDOI2009]晨跑 最小费用最大流
[BZOJ1877][SDOI2009]晨跑 Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现 ...
- 您需要安装旧 Java SE 6 运行环境才能打开“Eclipse”。
mac删除jdk: sudo rm -rf /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk 旧版本sdk地址: http://www.oracle. ...
- xcode7/ios9中 低版本app运行时,屏幕上下出现黑边的问题
xcode从低版本升级至 7.0或更高版本后,某些低版本app再次编译运行后,发现app在设备上运行时,会在上端和底部 出现黑边的现象.这导致app的展示界面跟缩水了一样,变得十分丑陋. 对于这一问题 ...
- NIO之Buffer的clear()、rewind()、flip()方法的区别
Java的NIO中有关Buffer的几种常用方法比如clear,rewind和flip到底有哪些区别.下面给大家这三种方法的源码,方便大家记忆.clear()方法用于写模式,其作用为情况Buffer中 ...
- postgresql----IN&&EXISTS
一.IN && NOT IN WHERE expression IN (subquery) 右边圆括号内是返回一个字段的子查询结果集,左边的表达式(或字段)对查询结果每一行进行一次运算 ...
- poj2176 Folding【区间DP】
Folding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1841 Accepted: 642 Special ...
- Tickets---hdu1260 (简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 题意就是有n个人排队买票,每个人需要的时间是a[i] (1=< i <=N),但是现 ...
- 【Loadrunner】平台1.9环境APP成功录制并调试成功后的脚本备份
1.录制相关Loadrunner及录制的APP所在手机网络代理相关设置请参考日志:http://www.cnblogs.com/zhuzhubaoya/p/9152022.html 2.调试成功的脚本 ...
- Mirror--生成用于镜像用户同步的脚本
USE master GO IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL DROP PROCEDURE sp_hexadecimal GO CREATE PR ...