当前位置:网站首页>D. Mark and Lightbulbs
D. Mark and Lightbulbs
2022-07-19 09:46:00 【Vegetable newbie】
D. Mark and Lightbulbs
The question
Give you two binary strings of the same length s
and t
, When s[i - 1] != s[i + 1]
You can flip s[i]
, Ask at least how many operations you can make s
Flip into t
, If not, output -1
.
Examination site
thinking
Ideas
- about 0 0 0 1 1 1 1 0 0 0
- We combine the same into a block , namely 0 1 0
- No matter how we operate , The number of blocks does not change .
- So let's calculate
s
andt
Number of blocks for , If the number of blocks is different , It's not feasible . - If possible , The contribution of each block is the absolute value of the subscript difference of the left endpoint of the block
Code
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
char s[N], t[N];
int n;
void solve(){
scanf("%d",&n);
scanf("%s", s + 1);
scanf("%s", t + 1);
vector<int>a, b;
for(int i = 1; i <= n; i ++ ){
if(s[i] != s[i - 1]) a.push_back(i);
if(t[i] != t[i - 1]) b.push_back(i);
}
if(s[1] != t[1] || s[n] != t[n] || a.size() != b.size()) puts("-1");
else{
long long ans = 0;
int len = a.size();
for(int i = 0; i < len; i ++ ) ans += abs(a[i] - b[i]);
printf("%lld\n", ans);
}
}
int main(){
int tt;
scanf("%d",&tt);
while(tt -- ){
solve();
}
return 0;
}
边栏推荐
- DuiLib 实现tooltip自定义鼠标提示窗口
- 光辉使用输出
- Un7.16: how to deploy projects on code cloud and invite team members?
- 【C语言】自定义类型初阶知识点
- Series operation of vector container (detailed explanation)
- Part I - Fundamentals of C language_ 6. Function
- [hero planet July training leetcode problem solving daily] 17th kuansou
- MySQL--SQL优化案例--隐式字符编码转换
- Anaconda与Jupyter Notebook入门级详细使用教程
- Why is SaaS so important for enterprise digital transformation?
猜你喜欢
ETH的拐点可能指日可待,这就是如何
el-table 列拖拽(无须引入其他插件)
Convert video format to GIF picture format
[C language] user defined type elementary knowledge points
Chapter 10 stack of STL
565. 数组嵌套
OLED displays how to understand the character sizes of 12*6, 16*8, 24*12, etc
Questions d'entrevue - concevoir des cas d'essai pour:: memcpy
【洛谷】P2357 守墓人
一汽丰田亚洲狮首次产品焕新
随机推荐
组件化高级--插槽
el-table 列拖拽(无须引入其他插件)
[Network Research Institute] the threat of machine learning system is time to take it seriously
Series operation of vector container (detailed explanation)
rhcsa 第一天 7.11
v-mode
第十三章 STL 之 set/ multiset
[C language] summary of array knowledge points
pip和pip3的区别用法详解
D. Mark and Lightbulbs
实验1:使用Matlab工具箱进行相机标定实验
Utility series - xshell installation, download and use
Chapter VIII vector of STL
Fundamentals of C language -- 2-1 pointer and wild pointer
OLED显示如何理解 12*6、16*8、24*12等字符大小
【C语言】数组知识点总结
Anaconda与Jupyter Notebook入门级详细使用教程
[hero planet July training leetcode problem solving daily] 17th kuansou
Un7.16: how to deploy projects on code cloud and invite team members?
CLWY权限管理(一)--- 项目搭建