前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >codeforces 1382C1(思维)

codeforces 1382C1(思维)

作者头像
dejavu1zz
发布于 2020-10-23 07:23:28
发布于 2020-10-23 07:23:28
27800
代码可运行
举报
运行总次数:0
代码可运行

题目描述

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代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
codeforces 1364B(数学)
Given a permutation p of length n, find its subsequence s1, s2, …, sk of length at least 2 such that:
dejavu1zz
2020/10/23
2730
codeforces 1364B(数学)
codeforces 1335E1+E2(思维)
数字出现的次数,这样就可以方便的找到出现次数最多的数字。然后枚举第一个和第三个区间即可,中间区间数字的个数也可以通过前缀和来计算
dejavu1zz
2020/10/23
2.5K0
codeforces 1335E1+E2(思维)
IME++ Starters Try-outs 2019 题解
显然如果有多棵树,则一定会存在无法到达的点。否则直接暴力 b f s bfs bfs求每个点到其余点的距离, a n s ans ans取 m a x max max即可
dejavu1zz
2020/12/02
5810
IME++ Starters Try-outs 2019 题解
codeforces 349B(贪心)
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya’s house. Igor thinks that the larger the number is, the more chance to win Tanya’s heart he has.
dejavu1zz
2020/10/23
2780
codeforces 1373C (数学)
如果cur<0时就会break,由于每次操作只会加一或者减一,每次break时cur的初始值都会加一,所以每次break是具有单调性的。我们可以用一个变量来记录cur的最小值,因为只有小于这个最小值时才会break,所以当每次小于最小值时计算ans的值。
dejavu1zz
2020/10/23
2660
codeforces 1144D(思维)
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
dejavu1zz
2020/10/23
3410
codeforces 1203D2(贪心)
image.png 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 sz(x)
dejavu1zz
2020/10/23
2170
codeforces 1203D2(贪心)
codeforces 1296E1(贪心+思维)
我们可以从前往后扫一遍,对于后面的字母,如果比当前字母小,则染成相反的颜色,如果比当前字母小且颜色和当前颜色相同,则说明不符合条件
dejavu1zz
2020/10/23
2370
codeforces 1296E1(贪心+思维)
codeforces 982C (dfs)
Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.
dejavu1zz
2020/10/23
3560
codeforces 1443B(思维)
题意描述 AC代码 #include<bits/stdc++.h> #define x first #define y second #define PB push_back #define mst(
dejavu1zz
2020/11/12
5030
codeforces 1443B(思维)
codeforces 1283D(BFS)
使用bfs来做,贪心的考虑,我们先找与n个点距离为1的圣诞树,然后再找距离为2的圣诞树,依次类推。
dejavu1zz
2020/10/23
2350
codeforces 1384A(构造)
The length of the longest common prefix of two strings s=s1s2…sn and t=t1t2…tm is defined as the maximum integer k (0≤k≤min(n,m)) such that s1s2…sk equals t1t2…tk.
dejavu1zz
2020/10/23
2940
codeforces 1426D(思维)
来储存每一步已经存在过的前缀和。如果在某个时刻,发现该前缀和已经出现过或已经为0,则说明需要插入一个足够大的数,也就意味着左边区间内的所有数被擦除,我们可以将set置空,重新开始统计。
dejavu1zz
2020/10/23
2400
codeforces 1426D(思维)
codeforces 1367D(思维)
Polycarp wrote on the board a string s containing only lowercase Latin letters (‘a’-‘z’). This string is known for you and given in the input.
dejavu1zz
2020/10/23
3020
codeforces 1417C(思维)
题意描述 思路 image.png AC代码 #include<bits/stdc++.h> #define x first #define y second #define PB push_back
dejavu1zz
2020/10/23
2880
codeforces 1417C(思维)
codeforces 1437D(思维)
题意描述 思路 AC代码 #include<bits/stdc++.h> #define x first #define y second #define PB push_back #define m
dejavu1zz
2020/10/30
3530
codeforces 1437D(思维)
codeforces 1405B(思维)
You’re given an array a of n integers, such that a1+a2+⋯+an=0.
dejavu1zz
2020/10/23
4140
codeforces 1399D
You are given a binary string s consisting of n zeros and ones.
dejavu1zz
2020/10/23
2700
codeforces 1328D(思维)
The round carousel consists of n figures of animals. Figures are numbered from 1 to n in order of the carousel moving. Thus, after the n-th figure the figure with the number 1 follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the i-th figure equals ti. You want to color each figure in one of the colors. You think that it’s boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.
dejavu1zz
2020/10/23
2960
codeforces 628B(数学)
Max wants to buy a new skateboard. He has calculated the amount of money that is needed to buy a new skateboard. He left a calculator on the floor and went to ask some money from his parents. Meanwhile his little brother Yusuf came and started to press the keys randomly. Unfortunately Max has forgotten the number which he had calculated. The only thing he knows is that the number is divisible by 4.
dejavu1zz
2020/10/23
3880
相关推荐
codeforces 1364B(数学)
更多 >
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验