当前位置:网站首页>Awoo's favorite problem (priority queue)
Awoo's favorite problem (priority queue)
2022-07-01 22:39:00 【A Fusu in the mountains】
Original link
Title Description
Here are two strings s and t, All the lengths are n. Each character in both strings is ’a’, ‘b’ or ’c’.
In one operation , You can do one of the following :
choice s What happened in “ab”, Replace it with “ba”;
choice s What happened in “bc”, And replace it with “cb”.
You can perform any number of moves ( It could be zero ). You can change the string s Make it equal to string t Do you ?
sample input
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
sample output
YES
NO
YES
YES
NO
Algorithm
( greedy + Priority queue + simulation )
Greedy strategy : Record the subscript of each letter through the priority queue , Each time if there are interchangeable letter pairs, take the nearest subscript and observe whether they can be exchanged , If not, the letter will pop up directly .
C++ Code
Time complexity O ( n l o g n ) O(nlogn) O(nlogn)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
ll a[N],s[N];
void solve()
{
int n;
cin >> n;
string s,t;
cin >> s >> t;
vector<int> v1(3,0),v2(3,0);
for (int i = 0;i < n;i ++ ) v1[s[i] - 'a'] ++;
for (int i = 0;i < n;i ++ ) v2[t[i] - 'a'] ++;
if (v1 == v2) {
priority_queue<int,vector<int>,greater<int>>a,b,c;
for(int i = 0;i < n;i ++ ) {
if(s[i] == 'a') a.push(i);
if(s[i] == 'b') b.push(i);
if(s[i] == 'c') c.push(i);
}
for (int i = 0;i < n;i ++ ) {
if (s[i] == t[i]) {
if (s[i] == 'a') a.pop();
else if (s[i] == 'b') b.pop();
else c.pop();
}else {
if (s[i] == 'a' && t[i] == 'b') {
if (!b.empty() && (c.empty() || b.top() < c.top())) {
int index = b.top();
b.pop();
swap(s[i],s[index]);
a.pop();
a.push(index);
continue;
}
}else if (s[i] == 'b' && t[i] == 'c') {
if (!c.empty() && (a.empty() || c.top() < a.top())) {
int index = c.top();
c.pop();
swap(s[i],s[index]);
b.pop();
b.push(index);
continue;
}
}
puts("NO");
return;
}
}
puts("YES");
}else puts("NO");
}
int main()
{
int t;
cin >> t;
while (t -- ) solve();
return 0;
}
边栏推荐
猜你喜欢

C#/VB.NET 给PDF文档添加文本/图像水印

【MySQL】explain的基本使用以及各列的作用

Training on the device with MIT | 256Kb memory

Clean up system cache and free memory under Linux

100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖

FFMpeg学习笔记

互联网的智算架构设计

Learn MySQL from scratch - database and data table operations

The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner

详解ThreadLocal
随机推荐
Object memory layout
使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏
信标委云原生专题组组长,任重道远!
Mask wearing detection method based on yolov5
牛客月赛-分组求对数和
Training on the device with MIT | 256Kb memory
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
Is PMP certificate really useful?
多种智能指针
Learn MySQL from scratch - database and data table operations
分享一个一年经历两次裁员的程序员的一些感触
Several ways of writing main function in C
MySQL之MHA高可用配置及故障切换
plantuml介绍与使用
【juc学习之路第8天】Condition
In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
keras训练的H5模型转tflite
Mysql——》Innodb存储引擎的索引
Slope compensation