【Codeforces】Codeforces Round 855 (Div. 3) (ABCDE) (补赛)

文章提供了五个不同的编程问题的解题思路和参考代码,包括判断字符串是否由重复的meow组成,计算字母对的数量,使用优先队列处理0和非0元素,删除重复字母以及判断字符串在一定操作下是否能相等。解题策略涉及字符串比较、贪心算法和数据结构的应用。
摘要由CSDN通过智能技术生成

Is It a Cat?

解题思路

模拟即可

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n;
string s;
void solve() {
	string ss = "meow";
	cin >> n >> s;
	if (n < 4) {
		cout << "NO" << endl;
		return;
	}
	for (int i = 0; i < n; i++) {
		if (s[i] <= 'Z' && s[i] >= 'A') {
			s[i] += ('a' - 'A');
		}
	}
	char x = s[0];
	int t = 0;
	if (x != 'm') {
		cout << "NO" << endl;
		return;
	}
	for (int i = 1; i < n; i++) {
		if (s[i] != x) {
			x = s[i];
			t++;
			if (x != ss[t] || t > 3) {
				cout << "NO" << endl;
				return;
			}
		}
	}
	if (t == 3) cout << "YES" << endl;
	else cout << "NO" << endl;
}
int main() {
	cin >> T;
	while (T--) solve();
	return 0;
}

Count the Number of Pairs

解题思路

贪心:统计每一种字母的大小写的数量,如果两者差值大于1,说明还可以消耗次数

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n, k;
string s;
unordered_map<char, int>mp;
void solve() {
	mp.clear();
	cin >> n >> k >> s;
	for (int i = 0; i < n; i++) mp[s[i]]++;
	ll cnt = 0;
	for (int i = 'a'; i <= 'z'; i++) {
		int j = i - ('a' - 'A');
		if (mp[i] < mp[j]) swap(mp[i], mp[j]);
		cnt += min(mp[i], mp[j]);
		while (k && mp[i] - mp[j] > 1) {
			k--;
			mp[i]--, mp[j]++;
			cnt++;
		}
	}
	cout << cnt << endl;
}
int main() {
	cin >> T;
	while (T--) solve();
	return 0;
}

Powering the Hero (hard version)

解题思路

贪心:每次遇到0,就取前i个种最大的,可以用优先队列实现

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n, x;
priority_queue<ll>q;
void solve() {
	while (!q.empty()) q.pop();
	cin >> n;
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		cin >> x;
		if (x) q.push(x);
		else {
			if (!q.empty()) {
				ans += q.top();
				q.pop();
			}
		}
	}
	cout << ans << endl;
}
int main() {
	cin >> T;
	while (T--) solve();
	return 0;
}

Remove Two Letters

解题思路

如果s_{i-1}=s_{i+1},那么答案计数减1

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n;
string s;
ll cnt;
void solve() {
	cin >> n >> s;
	cnt = n;
	for (int i = 1; i < n - 1; i++) {
		if (s[i - 1] == s[i + 1]) cnt--;
	}
	cout << cnt - 1 << endl;
}
int main() {
	cin >> T;
	while (T--) solve();
	return 0;
}

Unforgivable Curse (hard version)

解题思路

找规律,发现只有特定的位置可以互换,对于不能互换的位置,如果不相等,那么就是NO了

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n, k;
string a, b;
int dk1[4] = {0, 0, 1, 1};
int dk2[4] = {1, -1, 1, -1};
unordered_map<char, int>mpa, mpb;
void solve() {
	mpa.clear(), mpb.clear();
	cin >> n >> k >> a >> b;
	if (a == b) {
		cout << "YES" << endl;
		return;
	}
	for (int i = 0; i < n; i++) mpa[a[i]]++, mpb[b[i]]++;
	if (n <= k || mpa != mpb) {
		cout << "NO" << endl;
		return;
	}
	if (n >= 2 * k) {
		cout << "YES" << endl;
		return;
	}
	int x = n - k;
	for (int i = x; i <= n - x - 1; i++) {
		if (a[i] != b[i]) {
			cout << "NO" << endl;
			return;
		}
	}
	cout << "YES" << endl;
}
int main() {
	cin >> T;
	while (T--) solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值