Codeforces-989B - A Tide of Riverscape - 思维

91 篇文章 0 订阅

题解链接

http://www.lucien.ink/archives/269/


题目链接

http://codeforces.com/contest/989/problem/B


题目

The records are expressed as a string s of characters ‘0’, ‘1’ and ‘.’, where ‘0’ denotes a low tide, ‘1’ denotes a high tide, and ‘.’ denotes an unknown one (either high or low).

You are to help Mino determine whether it’s possible that after replacing each ‘.’ independently with ‘0’ or ‘1’, a given integer p is not a period of the resulting string. In case the answer is yes, please also show such a replacement to Mino.
In this problem, a positive integer p is considered a period of string s, if for all 1 ≤ i ≤ |s| − p, the i-th and (i + p)-th characters of s are the same. Here |s| is the length of s.

Input

The first line contains two space-separated integers n and p (1 ≤ p ≤ n ≤ 2000) — the length of the given string and the supposed period, respectively.
The second line contains a string s of n characters — Mino’s records. s only contains characters ‘0’, ‘1’ and ‘.’, and contains at least one ‘.’ character.

Output

Output one line — if it’s possible that p is not a period of the resulting string, output any one of such strings; otherwise output “No” (without quotes, you can print letters in any case (upper or lower)).


题意

  给你一个串,其中包含01.种字符,你可以在.的位置填入01,问你能不能不让这个串的循环周期为p,能的话输出填满之后的串,不能的话输出No


思路

  扫一遍,看看能不能使 x x x+p这两个位置的值不同即可。


实现

#include <bits/stdc++.h>
char str[2007];
int main() {
    int n, p;
    std::cin >> n >> p >> str;
    bool flag = false;
    for (int l = 0, r; (r = l + p) < n; l++) {
        if (str[l] != str[r]) {
            if (str[l] == '.') str[l] = ('1' - str[r]) + '0';
            else if (str[r] == '.') str[r] = ('1' - str[l]) + '0';
            flag = true;
            break;
        } else if (str[l] == '.') {
            str[l] = '1', str[r] = '0';
            flag = true;
            break;
        }
    }
    if (flag) {
        for (int i = 0; i < n; i++) {
            if (str[i] == '.') putchar('0');
            else putchar(str[i]);
        }
        putchar('\n');
    } else puts("No");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值