codeforces 1382C1(思维)

时间:2022-07-28
本文章向大家介绍codeforces 1382C1(思维),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目描述

This is the easy version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved.

There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In an operation, you select a prefix of a, and simultaneously invert the bits in the prefix (0 changes to 1 and 1 changes to 0) and reverse the order of the bits in the prefix.

For example, if a=001011 and you select the prefix of length 3, it becomes 011011. Then if you select the entire string, it becomes 001001.

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

思路

我们从后往前开始遍历,如果找到一个不同,那么就意味着肯定要反转,这时候还需要比较第一位与最后一位是否相同,如果相同的话那么第一位肯定要先反转一下。

AC代码

#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=1e6+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
string s1,s2;
int n;
void re(int idx){
    string cur=s1;
    rep(i,0,idx){
        if(cur[i]=='0') cur[i]='1';
        else cur[i]='0';
    }
    rrep(i,idx-1,0) s1[idx-i-1]=cur[i];
}
void solve(){
    vector<int> ans;
    cin>>n;
    cin>>s1>>s2;
    rrep(i,n-1,0){
        if(s1[i]!=s2[i]){
            if(s1[0]==s2[i] && i){
                ans.PB(1);
                re(1);
            }
            re(i+1);
            ans.PB(i+1);
        }
    }
    cout<<ans.size()<<' ';
    rep(i,0,ans.size()) cout<<ans[i]<<' ';
    cout<<endl;
}
int main(){
    IOS;
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}