codeforces 1504B Flip the Bits

链接:

https://codeforces.com/problemset/problem/1504/B

题意:

给两个长度相同的01串,对于每个0和1数量相等的字符串前缀,可以将它们翻转1变0,0变1。问,第一个字符串,是否能通过这种方式变成第二个字符串。

对于每一个0和1数量相等的字符串前缀,只要在内的对应数字相互异或值相等即可,最后判断一下剩下的数字,是否异或值均为0即可。我们把每个01串用一个数字cnt记录0和1是否相等,当为1是cnt++,当为0时,cnt--。每当cnt等于0时(即0和1相等),就重置异或值,供后面的数字判断异或值是否一样。

代码如下:

#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<random>
using namespace std;
typedef long long ll;
int main() {
	int T;
	cin >> T;
	while (T--) {
		int n;
		cin >> n;
		string s1, s2;
		cin >> s1 >> s2;
		int c1 = 0, c2 = 0;
		bool f = true;
		bool flag = true;
		int a;
		for (int i = 0; i < n; i++) {
			if (s1[i] == '1') {
				c1++;
			}
			else if (s1[i] == '0') {
				c1--;
			}
			if (s2[i] == '1') {
				c2++;
			}
			else if (s2[i] == '0') {
				c2--;
			}
			if (f) {
				a = ((s1[i] - '0') ^ (s2[i] - '0'));
				f = false;
			}
			else {

				if (a != ((s1[i] - '0') ^ (s2[i] - '0'))) {
					flag = false;
					break;
				}
				if (!c1) {
					f = true;
					if (c2) {
						flag = false;
						break;
					}
				}
			}
		}
		if (c1 && flag) {
			if (a) {
				flag = false;
			}
		}
		if (flag) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值