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;
}
- 【Golang语言社区】LollipopGo框架之商城项目--路由设置
- "LollipopGo/library/lollipop/common" 测试3
- "LollipopGo/library/lollipop/common" 测试2
- Golang语言并发技术详解
- Golang语言社区--Go基础课程第一节聊聊Go语言
- Spring boot with ELK(Elasticsearch + Logstash + Kibana)
- 数据库与缓存
- 通过 Proxy 汉化 restful 接口
- 学习笔记CB005:关键词、语料提取
- Eclipse油藏模型解析程序
- DBA生存警示:保护现场不要让事情更糟
- 爬虫快速入门
- “零基础学编程”都需要哪些基础?
- 零基础学编程008:print语句
- 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 数组属性和方法
- Python 技术篇-3行代码实现语音朗读,pywin32库实现语音朗读
- Java并发编程(2)- 线程安全性详解
- Python 技术篇-1行代码实现语音识别,speech库快速实现简单的语音对话
- linux和windows互传文件,用户配置文件和密码配置文件,用户组和用户管理
- 除Innodb和MyISAM外MySQL还有哪些存储引擎
- MySQL自身对性能的影响
- Python 技术篇-用pywin32库实现读写剪贴板
- Excel 技巧篇-在指定范围内生成指定小数位的随机数
- PyQt5 技巧篇-窗口置顶设置,如何使窗口始终显示在最前面
- 解决npm install时报错无法安装chromedriver的问题
- 解决集成jpa时无法创建entityManagerFactory的问题
- Spring船新版推出的WebFlux,是兄弟就来学我
- JDK9特性-Reactive Stream 响应式流
- Java函数式编程之Stream流编程
- 使用docker安装elasticsearch伪分布式集群以及安装ik中文分词插件