UVA - 11920 0 s, 1 s and ? Marks
Description
| 0 s, 1 s and ?
Marks |
Given a string consisting of 0, 1 and ?
only, change all the
? to 0/1, so that the size of the largest group is minimized. A group is a substring that contains either all zeros or all ones.
Consider the following example:
0 1 1 ? 0 1 0 ? ? ?
We can replace the question marks (?) to get
0 1 1 0 0 1 0 1 0 0
The groups are (0) (1 1) (0 0) (1) (0) (1) (0 0) and the corresponding sizes are 1, 2, 2, 1, 1, 1, 2. That means the above replacement would give us a maximum group size of 2. In fact, of all
the 24 possible replacements, we won't get any maximum group size that is smaller than 2.
Input
The first line of input is an integer T (T
5000) that indicates
the number of test cases. Each case is a line consisting of a string that contains
0, 1 and ?
only. The length of the string will be in the range [1,1000].
Output
Sample Input
4
011?010? ??
???
000111
00000000000000
Sample Output
Case 1: 2
Case 2: 1
Case 3: 3
Case 4: 14
题意:给定一个带问号的01串,把每一个问号替换成0或1。让最长的“连续的同样数字串”尽量短
思路:最大的最小採用二分。至于推断的时候,每一个位置要么是0要么是1,依据情况推断
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005; int dp[maxn][2];
char str[maxn];
int n; int check(int len) {
dp[0][0] = dp[0][1] = 0;
for (int i = 1; i <= n; i++) {
dp[i][0] = dp[i][1] = -1;
if (str[i] != '0') {
if (dp[i-1][0] >= 0)
dp[i][1] = 1;
if (dp[i-1][1] >= 0 && dp[i-1][1]+1 <= len && dp[i][1] == -1)
dp[i][1] = dp[i-1][1] + 1;
}
if (str[i] != '1') {
if (dp[i-1][1] >= 0)
dp[i][0] = 1;
if (dp[i-1][0] >= 0 && dp[i-1][0]+1 <= len && dp[i][0] == -1)
dp[i][0] = dp[i-1][0] + 1;
}
if (dp[i][1] == -1 && dp[i][0] == -1)
return 0;
}
return 1;
} int main() {
int t, cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%s", str+1);
n = strlen(str+1);
int l = 1, r = n;
while (l <= r) {
int m = l + r >> 1;
if (check(m))
r = m-1;
else l = m + 1;
}
printf("Case %d: %d\n", cas++, l);
}
return 0;
}
UVA - 11920 0 s, 1 s and ? Marks的更多相关文章
- UVA 624 (0 1背包 + 打印路径)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...
- UVa 127 - "Accordian" Patience POJ 1214 链表题解
UVa和POJ都有这道题. 不同的是UVa要求区分单复数,而POJ不要求. 使用STL做会比較简单,这里纯粹使用指针做了,很麻烦的指针操作,一不小心就错. 调试起来还是很费力的 本题理解起来也是挺费力 ...
- devices-list
转自:https://www.kernel.org/pub/linux/docs/lanana/device-list/devices-2.6.txt LINUX ALLOCATED DEVICES ...
- LightOJ 1141 Program E
Description In this problem, you are given an integer number s. You can transform any integer number ...
- Think Python Glossary
一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...
- 手眼标定eye-to-hand 示例:handeye_stationarycam_calibration
* * This example explains how to use the hand eye calibration for the case where* the camera is stat ...
- 第八节、图片分割之GrabCut算法、分水岭算法
所谓图像分割指的是根据灰度.颜色.纹理和形状等特征把图像划分成若干互不交迭的区域,并使这些特征在同一区域内呈现出相似性,而在不同区域间呈现出明显的差异性.我们先对目前主要的图像分割方法做个概述,后面再 ...
- 【Python】【自动化测试】【pytest】
https://docs.pytest.org/en/latest/getting-started.html#create-your-first-test http://www.testclass.n ...
- loadrunner 脚本开发-文件读写操作
脚本开发-文件读写操作 by:授客 QQ:1033553122 函数说明 函数原型: size_t fwrite( const void *buffer, size_t size, size_t co ...
随机推荐
- [机器学习] ML重要概念:梯度(Gradient)与梯度下降法(Gradient Descent)
引言 机器学习栏目记录我在学习Machine Learning过程的一些心得笔记,涵盖线性回归.逻辑回归.Softmax回归.神经网络和SVM等等,主要学习资料来自网上的免费课程和一些经典书籍,免费课 ...
- 【Android】9.3 自定义列表视图的外观
分类:C#.Android.VS2015: 创建日期:2016-02-18 一.简介 自定义的列表视图通常用Resources/Layout文件夹下的axml文件中的资源来声明,适配器则通过Id去加载 ...
- Modbus 通讯协议
摘要 工业控制已从单机控制走向集中监控.集散控制,如今已进入网络时代,工业控制器连网也为网络管理提供了方便.Modbus就是工业控制器的网络协议中的一种. 关键词 Modbus协议,串行通信,LRC校 ...
- js 过滤字符 和检测 特殊字符
// var str1 = str.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');// 去掉转义字符 // var str2= str.replace(/[\-\ ...
- dubbo的一些默认变量
dubbo默认变量表 变量名 描述 默认值 用途 DEFAULT_IO_THREADS 默认IO线程 Math.min(Runtime.getRuntime().availableProcessors ...
- C#集合概述
C#集合概述 2016-11-29 集合 顺序排列 连顺存储 直接访问方式 访问时间 操作时间 备注 Dictionary 是 Key Key:O(1) O(1) 访问性能最快,不支持排序 Sorte ...
- LINQ架构简单描述
写在前面的话:课堂上老师只是简单提了一下LINQ,当时听着老师对它的描述,感觉非常神奇,不用去操作繁琐的SQL语句了,读取数据库的操作居然能向写C#代码一样方便,但是一直没有机会去学习使用它. LIN ...
- struts2文件下载的编写步骤(文件导出)和输入流转换的方法
strut2文件下载三部曲:一个流.两个头信息 说明: ①一个流,在Action中需要一个getInputStream()的方法来获取下载的内容,其中inputStream是默认的,他会指示Strea ...
- ios UIImageView异步加载网络图片2
//1. NSData dataWithContentsOfURL // [self.icon setImage:[UIImage imageWithData:[NSData dataWithCont ...
- java质量提升相关
http://blog.sina.com.cn/s/blog_6814a1510102v2rp.html