题意:字符串长度为n,'.'可以变成‘0’或‘1’,问是否存在两个字符不相等。
由于数据范围<2000,所以我用的是暴力枚举的方法,即用for从头遍历,枚举所有可能出现的情况。
代码如下:
#include <stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
const int maxn = 1e7 + 10;
int n, p;
string s;
int main()
{
cin >> n >> p;
cin >> s;
for (int i = 0; i < n; ++i) {
if (i + p > n)break;
if (s[i] == '.'&&s[i + p] == '0') {
s[i] = '1';
for (int j = 0; j < n; ++j) {
if (s[j] == '.')s[j] = '0';
}
cout << s << endl;
return 0;
}
if (s[i] == '.'&&s[i + p] == '1') {
s[i] = '0';
for (int j = 0; j < n; ++j) {
if (s[j] == '.')s[j] = '1';
}
cout << s << endl;
return 0;
}
if (s[i] == '.'&&s[i + p] == '.') {
s[i] = '1';
s[i + p] = '0';
for (int j = 0; j < n; ++j) {
if (s[j] == '.')s[j] = '1';
}
cout << s << endl;
return 0;
}
if (s[i] == '1'&&s[i + p] == '.') {
s[i + p] = '0';
for (int j = 0; j< n; ++j) {
if (s[j] == '.')s[j] = '0';
}
cout << s << endl;
return 0;
}
if (s[i] == '0'&&s[i + p] == '.') {
s[i + p] = '1';
for (int j = 0; j < n; ++j) {
if (s[j] == '.')s[j] = '1';
}
cout << s << endl;
return 0;
}
if (s[i] == '1'&&s[i + p] == '0' || s[i] == '0'&&s[i + p] == '1') {
for (int j = 0; j < n; ++j) {
if (s[j] == '.')s[j] = '1';
}
cout << s << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}