目录
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 s s s and a parameter k k k, you need to check if there exist k + 1 k+1 k+1 non-empty strings a 1 , a 2 . . . , a k + 1 a_1,a_2...,a_{k+1} a1,a2...,ak+1, such that s = a 1 + a 2 + … + a k + a k + 1 + R ( a k ) + R ( a k − 1 ) + … + R ( a 1 ) . s=a_1+a_2+\ldots +a_k+a_{k+1}+R(a_k)+R(a_{k-1})+\ldots+R(a_{1}). s=a1+a2+…+ak+ak+1+R(ak)+R(ak−1)+…+R(a1).
Here + + + represents concatenation. We define R ( x ) R(x) R(x) as a reversed string x x x. For example R ( a b c d ) = d c b a R(abcd) = dcba R(abcd)=dcba. Note that in the formula above the part R ( a k + 1 ) R(a_{k+1}) R(ak+1) is intentionally skipped.
Input
The input consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1\le t\le 100 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 n n, k k k ( 1 ≤ n ≤ 100 1\le n\le 100 1≤n≤100, 0 ≤ k ≤ ⌊ n 2 ⌋ 0\le k\le \lfloor \frac{n}{2} \rfloor 0≤k≤⌊2n⌋) — the length of the string s s s and the parameter k k k.
The second line of each test case description contains a single string s s s of length n n n, consisting of lowercase English letters.
Output
For each test case, print “YES” (without quotes), if it is possible to find a 1 , a 2 , … , a k + 1 a_1,a_2,\ldots,a_{k+1} a1,a2,…,ak+1, 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 a 1 = q w a_1=qw a1=qw and a 2 = q a_2=q a2=q.
In the third test case, one possible solution is a 1 = i a_1=i a1=i and a 2 = o a_2=o a2=o.
In the fifth test case, one possible solution is a 1 = d o k i d o k i l i t e r a t u r e c l u b a_1=dokidokiliteratureclub a1=dokidokiliteratureclub.
题目大意 看懂题意的话可以直接跳过
把一个长度为n的字符串,分成 2 k + 1 2k+1 2k+1份小字符串。其中第 i i i个小字符串与第 2 k + 1 − i 2k+1-i 2k+1−i个字符串对称( a b c abc abc与 c b a cba cba对称),问能不能做到。
解题思路 发现规律
首先长度为 n n n要分成 2 k + 1 2k+1 2k+1份,就要满足 n ≥ 2 k + 1 n\ge2k+1 n≥2k+1。若不满足直接 N O NO NO。
下面讨论满足 n ≥ 2 k + 1 n\ge2k+1 n≥2k+1的情况:
其实找几个例子就能发现, s s s由3部分组成: A B C A B C ABC。其中
- A A A和 C C C是对称的
- A A A、 B B B、 C C C都能为空
- A 的长度 ≥ k A的长度\ge k A的长度≥k
样例分析
样例1
5 1
qwqwq
A
A
A :
q
q
q
B
B
B :
w
q
w
wqw
wqw
C
C
C :
q
q
q
满足上述3个条件,故输出YES
。
样例2
2 1
ab
2
<
1
∗
2
+
1
2<1*2+1
2<1∗2+1,不满足条件3,故输出NO
。
样例3
3 1
ioi
A
A
A :
i
i
i
B
B
B :
o
o
o
C
C
C :
i
i
i
满足上述3个条件,故输出YES
。
样例4
4 2
icpc
4
<
2
∗
2
+
1
4<2*2+1
4<2∗2+1,不满足条件3,故输出NO
。
样例5
22 0
dokidokiliteratureclub
A
A
A :
B
B
B :
d
o
k
i
d
o
k
i
l
i
t
e
r
a
t
u
r
e
c
l
u
b
dokidokiliteratureclub
dokidokiliteratureclub
C
C
C :
满足上述3个条件,故输出YES
。
样例6
19 8
imteamshanghaialice
无法找到两个个长度
≥
8
\ge 8
≥8的
A
A
A和
B
B
B使得
A
A
A、
B
B
B对称,故输出NO
。
样例7
6 3
aaaaaa
A
A
A :
q
q
q
B
B
B :
w
q
w
wqw
wqw
C
C
C : $$
6
<
3
∗
2
+
1
6<3*2+1
6<3∗2+1,不满足条件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;
}
原创不易,转载请附上原文链接哦~
Tisfy:https://blog.csdn.net/Tisfy/article/details/114647291