本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
Codeforces Round #706 (Div. 2)-A. Split it!
传送门 Time Limit: 1 second Memory Limit: 256 megabytes
Problem Description
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 and a parameter , you need to check if there exist non-empty strings , such that
Here represents concatenation. We define as a reversed string . For example . Note that in the formula above the part is intentionally skipped.
Input
The input consists of multiple test cases. The first line contains a single integer () — the number of test cases. The description of the test cases follows.
The first line of each test case description contains two integers , (, ) — the length of the string and the parameter .
The second line of each test case description contains a single string of length , consisting of lowercase English letters.
Output
For each test case, print "YES" (without quotes), if it is possible to find , and "NO" (without quotes) otherwise.
You can print letters in any case (upper or lower).
Sample Input
7
5 1
qwqwq
2 1
ab
3 1
ioi
4 2
icpc
22 0
dokidokiliteratureclub
19 8
imteamshanghaialice
6 3
aaaaaa
Sample Onput
YES
NO
YES
NO
YES
NO
NO
Note
In the first test case, one possible solution is and .
In the third test case, one possible solution is and .
In the fifth test case, one possible solution is .
题目大意 看懂题意的话可以直接跳过
把一个长度为n的字符串,分成份小字符串。其中第个小字符串与第个字符串对称(与对称),问能不能做到。
解题思路 发现规律
首先长度为要分成份,就要满足。若不满足直接。
下面讨论满足的情况:
其实找几个例子就能发现,由3部分组成:。其中
- 和是对称的
- 、、都能为空
样例分析
样例1
5 1
qwqwq
:
:
:
满足上述3个条件,故输出YES
。
样例2
2 1
ab
,不满足条件3,故输出NO
。
样例3
3 1
ioi
:
:
:
满足上述3个条件,故输出YES
。
样例4
4 2
icpc
,不满足条件3,故输出NO
。
样例5
22 0
dokidokiliteratureclub
:
:
:
满足上述3个条件,故输出YES
。
样例6
19 8
imteamshanghaialice
无法找到两个个长度的和使得、对称,故输出NO
。
样例7
6 3
aaaaaa
:
:
: $$
,不满足条件3,故输出NO
。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main()
{
int N;
cin >> N;
while (N--) //N组样例
{
int n, k;
cin >> n >> k;
string s;
cin >> s;
if (k * 2 >= n) //不满足条件3
{
puts("NO");
continue;
}
for (int i = 0; i < k; i++) //前面k个和后面k个相对称吗
{
if (s[i] != s[n - i - 1]) //不对称
{
puts("NO");
goto loop; //跳出
}
}
puts("YES");
loop:; //跳出到这里
}
return 0;
}
同步发文于我的CSDN,原创不易,转载请附上原文链接哦~
Tisfy:blog.csdn.net/Tisfy/artic…