Educational Codeforces Round 116 (Rated for Div. 2) A. AB Balance

发布时间 2023-10-14 14:40:23作者: zsxuan

给一个长为 \(n\) 的字符串 \(s\) ,只包含 \(0\) \(1\) 两种字符。定义 \(AB(s)\)\(s\) 中出现的 \(01\) 子串个数,\(BA(s)\)\(s\) 中出现的 \(10\) 子串个数。

在一步操作中,可以选择一个字符进行异或。询问最小的操作次数使得 \(AB(s) = BA(s)\)

显然连续的 \(11\) 或连续的 \(00\) 没有贡献。于是可以重 \(unique\) 的角度下考虑,\(s\) 成为一个 \(01\) 交替的字符串。

  • \(s_0 = s_n\) ,满足 \(AB(s) = BA(s)\)
  • 否则使用一步操作令 \(s_0 := s_n\) 即可。
view
#include <bits/stdc++.h>
typedef long long ll;
void solve(){
	std::string s;
	std::cin >> s; int n = s.length();
	if (s[0] != s[n - 1]) s[0] = s[n - 1];
	std::cout << s << '\n';
}
int main() {
	int _ = 1; std::cin >> _;
	while (_--) {solve();}
	return 0;
}