跳到正文

题解

题解:AT_arc194_e [ARC194E] Swap 0^X and 1^Y

aaa_Pigeon约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年4月29日
更新
2026年4月29日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

官解做法。

hint 1

先做一些基础观察。由于操作只是交换,所以 SS 和 TT 中 0 的数量要相等,1 同理(这不是废话吗)。

hint 2

旋转两个区间,一点都不好做。

我们可以怎样将旋转区间变成旋转两个相邻的“数”?比如,引入新的变量?

hint 3

由于是连续操作 XX 个 0 和 YY 个 1,考虑将连续 XX 个 0 变为 A,连续 YY 个 1 变为 B。

这样变的好处很明显:题目中的操作变为了交换 A B 和交换 B A。除此之外还有什么操作?

hint 4

A 还可以和相邻 0 交换,B 还可以和相邻的 1 交换。

这样子我们就将所有操作转换为 44 个数之间相邻交换了。

首先 SS 和 TT 中 0, 1, A, B 的数量要是相等的,除此之外,还有什么条件?

hint 5

由于 0 和 1 之间不能交换,所以 0 和 1 之间的顺序不会变。

换而言之,SS 和 TT 中的 0 1 串要是相同的。

但是 A 不能和 1 交换,B 不能和 0 交换,这能推出来什么?

hint 6

A 不能和 1 交换,说明两个 1 之间 A 的数量是不变的。所以每两个相邻 1 分割出的区间里,A 的数量不变(B 同理)。注意这里不要漏掉判断首尾边界。

然后这题就做完了 /hec。

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 500000;

int n, x, y;
string _s, _t, s, t;

int sa[N + 10], sb[N + 10];
int ta[N + 10], tb[N + 10];

int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> n >> x >> y;
	cin >> _s >> _t; _s = " " + _s, _t = " " + _t;

	s = " ", t = " ";

	for (int l = 1, r; l <= n; l = r + 1) {
		r = l;
		while (r < n && _s[r + 1] == _s[l]) ++r;
		int len = r - l + 1;

		if (_s[l] == '0') {
			for (int i = 1; i <= len / x; ++i) s += "A";
			for (int i = 1; i <= len % x; ++i) s += "0";
		}
		else {
			for (int i = 1; i <= len / y; ++i) s += "B";
			for (int i = 1; i <= len % y; ++i) s += "1";
		}
	}

	for (int l = 1, r; l <= n; l = r + 1) {
		r = l;
		while (r < n && _t[r + 1] == _t[l]) ++r;
		int len = r - l + 1;

		if (_t[l] == '0') {
			for (int i = 1; i <= len / x; ++i) t += "A";
			for (int i = 1; i <= len % x; ++i) t += "0";
		}
		else {
			for (int i = 1; i <= len / y; ++i) t += "B";
			for (int i = 1; i <= len % y; ++i) t += "1";
		}
	}

	if (s.size() != t.size()) {
		cout << "No"; return 0;
	}

	n = s.size() - 1;

	vector<int> s0, s1;
	vector<int> t0, t1;
	vector<int> s_all, t_all;

	s0.push_back(0), s1.push_back(0);

	for (int i = 1; i <= n; ++i) {
		if (s[i] == '0') s0.push_back(i), s_all.push_back(i);
		if (s[i] == '1') s1.push_back(i), s_all.push_back(i);
	}

	if (s0.back() != n) s0.push_back(n);
	if (s1.back() != n) s1.push_back(n);

	t0.push_back(0), t1.push_back(0);

	for (int i = 1; i <= n; ++i) {
		if (t[i] == '0') t0.push_back(i), t_all.push_back(i);
		if (t[i] == '1') t1.push_back(i), t_all.push_back(i);
	}

	if (t0.back() != n) t0.push_back(n);
	if (t1.back() != n) t1.push_back(n);

	if (s_all.size() != t_all.size()) {
		cout << "No"; return 0;
	}
	if (s0.size() != t0.size() || s1.size() != t1.size()) {
		cout << "No"; return 0;
	}

	for (int i = 0; i < s_all.size(); ++i) {
		if (s[s_all[i]] != t[t_all[i]]) {
			cout << "No"; return 0;
		}
	}

	sa[0] = sb[0] = ta[0] = tb[0] = 0;

	for (int i = 1; i <= n; ++i) {
		sa[i] = sa[i - 1] + (s[i] == 'A' ? 1 : 0);
		sb[i] = sb[i - 1] + (s[i] == 'B' ? 1 : 0);
		ta[i] = ta[i - 1] + (t[i] == 'A' ? 1 : 0);
		tb[i] = tb[i - 1] + (t[i] == 'B' ? 1 : 0);
	}

	for (int i = 1; i < s0.size(); ++i) {
		if (sb[s0[i]] - sb[s0[i - 1]] != tb[t0[i]] - tb[t0[i - 1]]) {
			cout << "No"; return 0;
		}
	}

	for (int i = 1; i < t1.size(); ++i) {
		if (sa[s1[i]] - sa[s1[i - 1]] != ta[t1[i]] - ta[t1[i - 1]]) {
			cout << "No"; return 0;
		}
	}

	cout << "Yes";

	return 0;
}
原文