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;
}
- RXTX for JAVA 串口通信
- Java集合源码分析(一)ArrayList
- 运行jar包找不到主类
- spring cloud 学习(7) - 生产环境如何不停机热发布?
- jupyter notebook 在mac OS上的安装
- Hadoop(七)HDFS容错机制详解
- java一些常用并发工具示例
- 机器人来“抢工作”了,瑞典人为何能淡定处之
- 线性代数01 线性的大脑
- spring cloud 学习(10) - 利用springfox集成swagger
- Hadoop(六)之HDFS的存储原理(运行原理)
- Blockchain Global CEO Sam Lee:以大数据为基础,区块链技术加快决策生成速度
- Hadoop(五)搭建Hadoop客户端与Java访问HDFS集群
- 被解放的姜戈08 远走高飞
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 【React+Typescript+Antd】防止样式感染——LESS CSS 框架简介
- 【React+Typescript+Antd】界面框架布局——Layout布局+ Grid栅格
- SpringBoot源码学习(三)
- Axure RP 9 安装与中文汉化
- Linux 离线安装docker的过程
- Mac环境变量设置(以ADB为例)
- Linux 基础之定时任务
- Git库迁移步骤(从服务器A迁移至服务器B)
- springboot源码解析(四)
- 第2天:网易2018年校园招聘NLP算法工程师笔试试卷分析(二)
- Windows下Scoop安装、配置与使用
- Flutter免费iOS真机调试 AndroidStudio iPhone真机运行教程
- Flutter GridView 网格控件
- Flutter ListView 下拉刷新,上拉加载更多
- Flutter问题:import 'package:english_words/english_words.dart'失败