牛客周赛125#题解#小苯的01合并
小苯的01合并
https://ac.nowcoder.com/acm/contest/126319/D
思路:按题意输入数据后,我们用双端队列来存输入的两个字符串(需要转为数字哦),如果队首相同我们就弹出队首,否则,因为题目说只能对字符串A进行操作合并,所以我们合并队首和队首的下一个元素,即进行异或运算,因为题目保证了A,B字符串的长度起码是相等的,所以上面的这个判断即操作持续到两字符串一方长度为0,如果A字符串有剩余,我就对字符串A剩余字符进行异或运算,如果运算结果为0,那么就输出YES,否则输出NO.
----若实在听不太懂,请移步至官方视频讲解链接-->,我的思路是听它讲的,如我的思路对大家有所帮助请留下一个免费的点赞吧! --若有不足的地方望支出,我将严肃改造!
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int t;
cin >> t;
while (t--) {
int n, m;
string A, B;
cin >> n >> m;
cin >> A >> B;
deque<int> q1, q2;
for (auto &i : A) {
q1.push_back(i - '0');
}
for (auto &j : B) {
q2.push_back(j - '0');
}
while (!q1.empty() && !q2.empty()) {
if (q1.front() == q2.front()) {
q1.pop_front();
q2.pop_front();
} else {
if (q1.size() < 2) {
break;
}
int x = q1.front();
q1.pop_front();
q1.front() ^= x;
}
}
int mind = 0;
while (!q1.empty()) {
mind ^= q1.front();
q1.pop_front();
}
if (q2.empty() && mind == 0) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}
查看24道真题和解析

