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;
}