每日一题 第九十五期 Codeforces Round 905 (Div. 3)

本文介绍了一个编程问题,要求检查给定字符串通过删除恰好k个字符后是否可以重新排列形成回文。输入是一个包含n个小写字母的字符串和k个要删除的字符数,输出是YES或NO,表示是否可以形成回文。
摘要由CSDN通过智能技术生成

B. Chemistry

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given a string s s s of length n n n, consisting of lowercase Latin letters, and an integer k k k.

You need to check if it is possible to remove exactly k k k characters from the string s s s in such a way that the remaining characters can be rearranged to form a palindrome. Note that you can reorder the remaining characters in any way.

A palindrome is a string that reads the same forwards and backwards. For example, the strings “z”, “aaa”, “aba”, “abccba” are palindromes, while the strings “codeforces”, “reality”, “ab” are not.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of the test cases. This is followed by their description.

The first line of each test case contains two integers n n n and k k k (KaTeX parse error: Expected 'EOF', got '&' at position 10: 0 \leq k &̲lt; n \leq 10^5) — the length of the string s s s and the number of characters to be deleted.

The second line of each test case contains a string s s s of length n n n, consisting of lowercase Latin letters.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output “YES” if it is possible to remove exactly k k k characters from the string s s s in such a way that the remaining characters can be rearranged to form a palindrome, and “NO” otherwise.

You can output the answer in any case (uppercase or lowercase). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as positive answers.

Example
inputCopy
14
1 0
a
2 0
ab
2 1
ba
3 1
abb
3 2
abc
6 2
bacacd
6 2
fagbza
6 2
zwaafa
7 2
taagaak
14 3
ttrraakkttoorr
5 3
debdb
5 4
ecadc
5 3
debca
5 3
abaac
outputCopy
YES
NO
YES
YES
YES
YES
NO
NO
YES
YES
YES
YES
NO
YES

Note

In the first test case, nothing can be removed, and the string “a” is a palindrome.

In the second test case, nothing can be removed, but the strings “ab” and “ba” are not palindromes.

In the third test case, any character can be removed, and the resulting string will be a palindrome.

In the fourth test case, one occurrence of the character “a” can be removed, resulting in the string “bb”, which is a palindrome.

In the sixth test case, one occurrence of the characters “b” and “d” can be removed, resulting in the string “acac”, which can be rearranged to the string “acca”.

In the ninth test case, one occurrence of the characters “t” and “k” can be removed, resulting in the string “aagaa”, which is a palindrome.

AC代码:

#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=9901;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;
 
int t;
int k;
string s;
int a[30];
int main()
{
	cin >> t;
	int n;
	while(t --){
		cin >> n >> k;
		cin >> s;
		memset(a, 0 ,sizeof a);
		for(int i = 0; i < n; i ++)
		{
			a[s[i] - 'a'] ++;
		}
		int cnt = 0;
		for(int i = 0; i <= 26; i ++)
		{
			if(a[i] & 1)cnt ++;
		}
		if(cnt - k <= 1) puts("YES");
		else puts("NO");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值