Codeforces Round #658 (Div. 2) C1. Prefix Flip (Easy Version)

原题地址

https://codeforces.com/contest/1382/problem/C1

解题思路

当时做的时候没做出来,补题的时候茅厕顿开直呼内行

题目里原来有提示了啊!!

Your task is to transform the string a into b in at most 3n operations. It can be proved that it is always possible. 

因为每次翻转的是前缀,后面的是固定的,所以从后往前进行变换,当前位置如果不同,就进行如下的3次变换

例如:

a: 01001

b:10000

从最后一位开始判断,1与0不同,则执行下列变换:

第1次:从开头一直到当前位置的这个串进行变换

a->01101

第2次:a的首个位置进行变换

->11101

第3次:重复第1次的变换

->01000

可以看到最后一位就神奇地变为了0,并且前面的保持不变~那么接下来从右向左重复执行上面的操作就可以拉!

参考代码

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef double db;
typedef long long LL;
typedef vector<int> VI;
const int inf = 2e9;
const LL INF = 8e18;
const int maxn = 5e5 + 5;

int main() {
	int t, n;
	string a, b;
	cin >> t;
	while (t--) {
		cin >> n >> a >> b;
		int ans = 0;
		for (int i = n - 1; i >= 0; --i) {
			if (a[i] != b[i]) ans++;
		}
		cout << 3 * ans << ' ';
		for (int i = n - 1; i >= 0; --i) {
			if (a[i] != b[i]) {
				cout << i + 1 << " 1 " << i + 1 << ' ';
			}
		}
		cout << endl;
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值