[C 语言笔记--Day03] 解题纪录:MIN-MEX Cut

题目:MIN-MEX Cut

观察:

  1. 当 s 全为 0 时,MEX = 1
  2. 当 s 全为 1 时,MEX = 0
  3. 由於 1, 2 两点, s = 0000111110000 跟 s = 010 可以视为同样的状况
  4. 当 s 很长且很杂乱时,(如:00101010001010101)就只要切成单一一个 substring,让 MEX = 2,就行了

解题:

由於观察 3、4两点 ,推断用穷举的方式应该不会太复杂,分别举出 s[0] == '0's[0] == '1' 的情况:

s minimal sum of MEX
0 1
01 1
010 2
0101 2
01010 2
010101 2
0101010 2
s minimal sum of MEX
1 0
10 1
101 1
1010 2
10101 2
101010 2
1010101 2
10101010 2

依照这两张表,把程序写出来就好

程序码:

// https://codeforces.com/contest/1566/problem/B
#include <stdio.h>

#define SIZE (int)1e5

int count_part(char *s)
{
    int result = 1;
    char *p = s;
    while (*(++p) != '\0')
        if (*p != *(p-1))
            result++;
    return result;
}

int main()
{
    int t, parts;
    scanf("%d\n", &t);

    while (t--) {
        char s[SIZE+1];
        scanf("%s", s);
        parts = count_part(s);

        if (parts == 1) {
            if (*s == '0')
                printf("1");
            else
                printf("0");
        } else if (parts == 2) {
            printf("1");
        } else if (parts == 3 && *s == '1') {
            printf("1");
        } else {
            printf("2");
        }
        printf("\n");
    }

    return 0;
}

<<:  DAY12-JAVA的类别(6)-变数和函数

>>:  Day 08: Python额外知识小补充

[Day16] 依据反馈改进初始对话流

从昨天的文章中,我们获得了数种进行绿野仙踪实验的方法 在今日的文章,假定我们已经获取用户的反馈。并...

成为工具人应有的工具包-01 FullEventLogView

Windows Event Log & FullEventLogView LOG 是监识调查...

【Day9】To be or Not To be?逻辑运算子

逻辑运算子(Logical Operator)有 AND &&、OR ||、NOT...

Gulp 基础介绍 DAY79

今天我们要先来介绍 Gulp 基本的四个 API 提供使用 gulp.task 执行工作 gulp....

DAY13 资料室--Vuex载荷Payload

前言 为什麽要特别提一下 Payload ? 是因为像 mutations 跟 actions,其实...