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 (T5000) 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

For each case, output the case number first followed by the size of the minimized largest group.

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的更多相关文章

  1. UVA 624 (0 1背包 + 打印路径)

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...

  2. UVa 127 - &quot;Accordian&quot; Patience POJ 1214 链表题解

    UVa和POJ都有这道题. 不同的是UVa要求区分单复数,而POJ不要求. 使用STL做会比較简单,这里纯粹使用指针做了,很麻烦的指针操作,一不小心就错. 调试起来还是很费力的 本题理解起来也是挺费力 ...

  3. devices-list

    转自:https://www.kernel.org/pub/linux/docs/lanana/device-list/devices-2.6.txt LINUX ALLOCATED DEVICES ...

  4. LightOJ 1141 Program E

    Description In this problem, you are given an integer number s. You can transform any integer number ...

  5. Think Python Glossary

    一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...

  6. 手眼标定eye-to-hand 示例:handeye_stationarycam_calibration

    * * This example explains how to use the hand eye calibration for the case where* the camera is stat ...

  7. 第八节、图片分割之GrabCut算法、分水岭算法

    所谓图像分割指的是根据灰度.颜色.纹理和形状等特征把图像划分成若干互不交迭的区域,并使这些特征在同一区域内呈现出相似性,而在不同区域间呈现出明显的差异性.我们先对目前主要的图像分割方法做个概述,后面再 ...

  8. 【Python】【自动化测试】【pytest】

    https://docs.pytest.org/en/latest/getting-started.html#create-your-first-test http://www.testclass.n ...

  9. loadrunner 脚本开发-文件读写操作

    脚本开发-文件读写操作 by:授客 QQ:1033553122 函数说明 函数原型: size_t fwrite( const void *buffer, size_t size, size_t co ...

随机推荐

  1. Android Gradle 引用本地 AAR 的几种方式

    折衷方案: 1.方式2 - 不完美解决办法2 2.再使用"自定义Gradle代码"来减轻重复设置的问题. 自定义Gradle代码如下: repositories { flatDir ...

  2. TP关联模型

    <?php class ExpertiseModel extends RelationModel { protected $_link=array( 'Role'=> array( 'ma ...

  3. Linux环境下搭建测试环境(LAMP详细说明)

    一.安装虚拟机与CentOS7 传送门:https://www.cnblogs.com/mrgavin/p/9372393.html 注意:以下安装,我都是用的root权限. 二.安装Apache1. ...

  4. Java并发:volatile内存可见性和指令重排

    volatile两大作用 1.保证内存可见性 2.防止指令重排 此外需注意volatile并不保证操作的原子性. (一)内存可见性 1 概念 JVM内存模型:主内存和线程独立的工作内存 Java内存模 ...

  5. SourceInsight-Symbol not found

    使用SourceInsight查看源代码时,发现点击查看相关类型时,无法关联到其代码,出现 symbol not found, 然而明明在我的头文件有定义的 网上查了一下主要是因为新建工程导入文件后, ...

  6. version `GLIBC_2.14' not found 解决方法.

    from http://blog.csdn.net/force_eagle/article/details/8684669 version `GLIBC_2.14' not found 解决方法. 一 ...

  7. java中Logger.getLogger(Test.class)

    java中Logger.getLogger(Test.class) log4的使用方法: log4是具有日志记录功能,主要通过一个配置文件来对程序进行监测有两种配置方式:一种程序配置,一种文件配置有三 ...

  8. python 合并字典,相同 key 的 value 如何相加?

    x = { 'apple': 1, 'banana': 2 } y = { 'banana': 10, 'pear': 11 } 需要把两个字典合并,最后输出结果是: { 'apple': 1, 'b ...

  9. Linux while 获取键盘输入退出

    c 语言实现如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  10. JDBC 查询的三大参数 setFetchSize prepareStatement(String sql, int resultSetType, int resultSetConcur)

    JDBC 查询的三大参数 本文转载至 http://blog.csdn.net/turkeyzhou/article/details/5115228 DBC1.0 .JDBC2.0 .JDBC3.0  ...