codeforces 989B(A Tide of Riverscape)

题意:字符串长度为n,'.'可以变成‘0’或‘1’,问是否存在两个字符不相等。

由于数据范围<2000,所以我用的是暴力枚举的方法,即用for从头遍历,枚举所有可能出现的情况。

代码如下:

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int maxn = 1e7 + 10;
int n, p;
string s;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	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;
}

猜你喜欢

转载自blog.csdn.net/zsnowwolfy/article/details/82734949
今日推荐