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 ...
随机推荐
- SD卡WAV音乐播放器(quartus11.0)(FAT32)(DE2-115)
准备工具:格式工厂,Windows录音机,SD卡(小于等于2G),音箱 首先,选一首MP3,用格式工场转化成WAV格式.可以看到转化后的文件变得很大,因为WAV就是AD采样值加个文件头,所以数据量巨大 ...
- 清理parallels desktop 卸载后的图标残留
卸载parallels后, 之前windows上打开过的文件图标还是带两条竖线,CNM! 解决办法: 1.终端输入: /System/Library/Frameworks/CoreServices.f ...
- spark内存概述
转自:https://github.com/jacksu/utils4s/blob/master/spark-knowledge/md/spark%E5%86%85%E5%AD%98%E6%A6%82 ...
- Maven-7:Maven配置编译的字符集方法
http://blog.csdn.net/jsjszg/article/details/9237189 ************************************************ ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- python 合并字典,相同 key 的 value 如何相加?
x = { 'apple': 1, 'banana': 2 } y = { 'banana': 10, 'pear': 11 } 需要把两个字典合并,最后输出结果是: { 'apple': 1, 'b ...
- iOS 键盘处理(改变键盘为完成键),UITextField键盘显示隐藏,弹出,回弹
很多时候用到UITextField时,处理键盘是一个很棘手的问题. 问题一:如何隐藏键盘? 方案1.改变键盘右下角的换行(enter)键为完成键,后实现代理方法键盘自动回弹 keyBoardContr ...
- c# 终止线程
最近在弄一个等待窗口,使用了线程去调用form.在结束线程这边碰到了些问题.调用: thread.Abort();thread.Join();老被ThreadAbortException异常抛出困扰. ...
- Android——Button的颜色
.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- 关于最新的Vuforia-unity3d-samples2-8-13
今天用了一下最新的Vuforia for unity3d的样例2-813版本号.导入我的unity3d之后.发现ARCamera的检视面板下非常多熟悉的元素都不见了,根本没法改动标志,如图所看到的. ...