当前位置:网站首页>awoo‘s Favorite Problem(优先队列)
awoo‘s Favorite Problem(优先队列)
2022-07-01 21:45:00 【山中一扶苏】
原题链接
题目描述
给你两个字符串s和t,长度都是n。两个字符串中的每个字符都是’a’, ‘b’或’c’。
在一个操作中,您可以执行以下操作之一:
选择s中出现的“ab”,将其替换为“ba”;
选择s中出现的“bc”,并将其替换为“cb”。
你可以执行任意数量的移动(可能是零)。 你能改变字符串s让它等于字符串t吗?
输入样例
5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab
输出样例
YES
NO
YES
YES
NO
算法
(贪心 + 优先队列 + 模拟)
贪心策略:通过优先队列记录每个字母的下标,每次如果有可换的字母对则取最近的下标观察是否可以交换,没有则直接弹出该字母。
C++ 代码
时间复杂度 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;
}
边栏推荐
- 使用闭包实现点击按钮切换 toggle
- Communication between browser tab pages
- 收到一封CTO来信,邀约面试机器学习工程师
- Make a three digit number of all daffodils "recommended collection"
- K-means based user portrait clustering model
- Talking from mlperf: how to lead the next wave of AI accelerator
- 函数基本学习之一
- Classify boost libraries by function
- spark analyze命令使用及其作用 map join broadcast join 广播join
- LIS (longest ascending subsequence) problem that can be understood [easy to understand]
猜你喜欢

Mask wearing detection method based on yolov5

【目标跟踪】|单目标跟踪指标

Flume interview questions

Recent public ancestor offline practice (tarjan)

News classification based on LSTM model

上半年暂停考试要补考?包含监理工程师、建筑师等十项考试
![[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!](/img/27/4bd0de716f5cb360d54f140dc8e9c1.png)
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!

高攀不起的希尔排序,直接插入排序

MySQL series transaction log redo log learning notes

K-means based user portrait clustering model
随机推荐
ICML2022 | 基于元语义正则化的介入性对比学习
Aidl basic use
详解LockSupport的使用
Wechat applet, continuously playing multiple videos. Synthesize the appearance of a video and customize the video progress bar
flink sql-client 使用 对照并熟悉官方文档
MySQL learning notes - SQL optimization of optimization
Little p weekly Vol.11
Using closures to switch toggle by clicking a button
plantuml介绍与使用
Fundamentals - IO intensive computing and CPU intensive computing
Which securities company should we choose to open an account for flush stock? Is it safe to open an account with a mobile phone?
Gaussdb (DWS) active prevention and troubleshooting
GenICam GenTL 标准 ver1.5(4)第五章 采集引擎
Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc
Simple interactive operation of electron learning (III)
详解ThreadLocal
What is the difference between consonants and Initials? (difference between initials and consonants)
Mysql——》Innodb存储引擎的索引
企业架构与项目管理的关联和区别
Pytest collection (2) - pytest operation mode