zoukankan      html  css  js  c++  java
  • UVA

    Description

    Download as PDF


     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$ le$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

    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
    ", cas++, l);
    	}
    	return 0;
    }



  • 相关阅读:
    医学-药物-未分类-糠酸莫米松鼻喷雾剂
    Delphi 错误提示: Unknown picture file extension (.jpg)
    SQL SERVER 两表比对更新、插入字段写法
    医学-药物-非激素类抗炎药-孟鲁司特钠片
    医学-药物-分类说明-抗组胺药
    医学-药物-抗组胺药-富马酸酮替芬片
    Delphi 判断字符串是否是数字、大小字母、小写字母、纯字母组成
    医学-药物-未分类-复方甲氧那明胶囊
    医学-药物-分类说明-消炎药
    计算机语言,学习心态
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6727813.html
Copyright © 2011-2022 走看看