Codeforces Round #706 (Div. 2) A. Split it!(贪心/思维)
A. Split it!
Kawashiro Nitori is a girl who loves competitive programming.
One day she found a string and an integer. As an advanced problem setter, she quickly thought of a problem.
Given a string 𝑠s and a parameter k, you need to check if there exist k+1 non-empty strings a1,a2...,ak+1, such that
s=a1+a2+…+ak+ak+1+R(ak)+R(ak−1)+…+R(a1).
Here ++ represents concatenation. We define R(x) as a reversed string x. For example R(abcd)=dcba. Note that in the formula above the part R(ak+1) is intentionally skipped.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case description contains two integers n, k (1≤n≤100, 0≤k≤⌊n2⌋) — the length of the string 𝑠s and the parameter k.
The second line of each test case description contains a single string s of length 𝑛n, consisting of lowercase English letters.
Output
For each test case, print "YES" (without quotes), if it is possible to find 𝑎1,𝑎2,…,𝑎𝑘+1a1,a2,…,ak+1, and "NO" (without quotes) otherwise.
You can print letters in any case (upper or lower).
Example
input
Copy
7
5 1
qwqwq
2 1
ab
3 1
ioi
4 2
icpc
22 0
dokidokiliteratureclub
19 8
imteamshanghaialice
6 3
aaaaaa
output
Copy
YES
NO
YES
NO
YES
NO
NO
水题,原问题等价于判断从字符串最中间拿走一部分子串后剩下的字符串是否回文(贪心地考虑,若s为ab + cde + xy + edc + ba满足条件,而若取a + b + c + dexyed + d + b + a显然也满足条件,类似两个前缀相等,则其等长的前缀也相等),因此从头尾取k个字符比较即可,注意判断n为偶数且k = n / 2的情况。
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <set>
#include <map>
using namespace std;
int main() {
freopen("data.txt", "r", stdin);
int t;
cin >> t;
while(t--) {
int n, k;
cin >> n >> k;
string s;
cin >> s;
bool flag = 1;
for(int i = 0; i < k; i++) {
if(s[i] != s[s.size() - 1 - i]) {
flag = 0;
break;
}
}
if(!(n & 1) && k == n / 2) flag = 0;
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-03-10 Educational Codeforces Round 83 (Rated for Div. 2) C. Adding Powers(数学)
2020-03-10 Educational Codeforces Round 83 (Rated for Div. 2) B. Bogosort(排序/思维)
2020-03-10 Educational Codeforces Round 83 (Rated for Div. 2) A. Two Regular Polygons(水题)