current position:Home>A. Two 0-1 Sequences (greedy)
A. Two 0-1 Sequences (greedy)
2022-08-06 09:23:45【Luo gkv】
题意
给定a,b这两个01字符串
现a可以执行操作
选取a的头两个字符,and delete one of them,Reassemble the remaining elements into a new string.
即a = a[0] + a[2:]或者a = a[1] + a[2:]
Ask through the above operations several times,能否将a转化为b.
思路
aof each operation,Essentially after removing a few characters from the prefix,can finally andb相等.
We are greedy for reservationsa和bSuffix the same part.
- 如果b和a的最长公共后缀,小于len(b)-2,此时a和b至少有2个字符不同,直接gg
- 如果b和a的最长公共后缀,等于len(b)-1,此时a和b有1个字符不同.我们只需看athe remaining prefixes,是否有等于b[0]的元素即可.
- 如果b和a的最长公共后缀,等于len(b),Clearly there is a solution at this point.
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 55;
int n, m;
char a[maxn], b[maxn];
void solve() {
scanf("%d%d", &n, &m);
scanf("%s%s", a, b);
int i = n - 1, j = m - 1;
while (i >= 0 && j >= 0) {
if (a[i] != b[j]) {
break;
}
--i;
--j;
}
int flag = 0;
while (i >= 0) {
if (a[i] == '0') {
flag |= 1;
} else {
flag |= 2;
}
--i;
}
bool ok = j < 0;
ok |= (j == 0 && flag == 3);
printf("%s\n", ok ? "YES" : "NO");
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
}
最后
weixin gongzhonghao搜 对方正在debug,关注下,一起快乐刷题吧~
copyright notice
author[Luo gkv],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/218/202208060918144665.html
The sidebar is recommended
- 2022 Hailiang SC Travel Notes
- dalle2: hierarchical text-conditional image generation with clip
- Tencent Cloud VOD uploads video files to solve the path problem
- LeetCode - 1047. Remove all adjacent duplicates in a string
- 2022-08-05: What does the following go code output?A: 65, string; B: A, string; C: 65, int; D: error.
- LeetCode - 345. The reversal in the string vowels
- Page Loading Animation_Gradient Color Rotating Small Circle
- 韩流体小球加载动画
- Card hovering frosted glass effect
- How does the data security law apply to enterprises?
guess what you like
Full screen digital preload animation
Day 16 (Configuration BPDU, TCN BPDU)
VLAN experiment
ROS error [rospack] Error: package ‘.....‘ not found
Token design scheme under microservice
Combination of Leetcode77.
Native js implements table table
Day 17 (16 day bpdus related knowledge and STP configuration)
Native js implements mouse following to display floating box information
Exchange comprehensive experiment (to be supplemented)
Random recommended
- Detailed explanation of Mysql things (important)
- Linux - several ways to install MySQL
- /var/log/messages is empty
- The 22nd day of the special assault version of the sword offer
- Stone Atom Technology officially joined the openGauss community
- 18 days (link aggregation of configuration, the working process of the VRRP, IPV6 configuration)
- From "prairie cattle" to "digital cattle": Mengniu's digital transformation!
- Summary of the experience of project operation and maintenance work
- WPF - Styles and Templates
- BigEvent Demo
- rain cloud animation
- VS namespace names of different projects of the same solution are unique
- Flashing Neon Text Animation
- ACM common header files
- Free and open source web version of Xshell [Happy New Year to everyone]
- Timed task appears A component required a bean named ‘xxx‘ that could not be found
- Two important self-learning functions in pytorch dir(); help()
- [Mathematical Modeling] Linear Programming
- Folyd
- 【Untitled】
- HCIP 18 days notes
- The web version of Xshell supports FTP connection and SFTP connection
- The values in the array into another array, and capital
- Remember to deduplicate es6 Set to implement common menus
- View the Linux log on the web side, and view the Linux log on the web side
- 21-day Learning Challenge--Pick-in on the third day (dynamically change the app icon)
- Xshell download crack, the history of the most simple tutorial
- How is the LinkedList added?
- Web version Xshell supports FTP connection and SFTP connection [Detailed tutorial] Continue from the previous article
- Usage of torch.utils.data in pytorch ---- Loading Data
- Experiment 9 (Exchange Comprehensive Experiment)
- [Mathematical Modeling] Integer Programming
- "Introduction to nlp + actual combat: Chapter 9: Recurrent Neural Network"
- Expansion mechanism of ArrayList
- (5) BuyFigrines Hd 2022 school training
- [Nanny-level tutorial] How does Tencent Cloud obtain secretId and secretKey, and enable face service
- RL reinforcement learning summary (2)
- ELT.zip 】 【 OpenHarmony chew club - the methodology of academic research paper precipitation series
- Hdu 2022 Multi-School Training (5) Slipper
- Dijkstr heap optimization