Codeforces Round #706 (Div. 2)-A. Split it!-题解

131 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

@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 ss and a parameter kk, you need to check if there exist k+1k+1 non-empty strings a1,a2...,ak+1a_1,a_2...,a_{k+1}, such that s=a1+a2++ak+ak+1+R(ak)+R(ak1)++R(a1).s=a_1+a_2+\ldots +a_k+a_{k+1}+R(a_k)+R(a_{k-1})+\ldots+R(a_{1}).

Here ++ represents concatenation. We define R(x)R(x) as a reversed string xx. For example R(abcd)=dcbaR(abcd) = dcba. Note that in the formula above the part R(ak+1)R(a_{k+1}) is intentionally skipped.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1t1001\le t\le 100) — the number of test cases. The description of the test cases follows.

The first line of each test case description contains two integers nn, kk (1n1001\le n\le 100, 0kn20\le k\le \lfloor \frac{n}{2} \rfloor) — the length of the string ss and the parameter kk.

The second line of each test case description contains a single string ss of length nn, consisting of lowercase English letters.

Output

For each test case, print "YES" (without quotes), if it is possible to find a1,a2,,ak+1a_1,a_2,\ldots,a_{k+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 a1=qwa_1=qw and a2=qa_2=q.

In the third test case, one possible solution is a1=ia_1=i and a2=oa_2=o.

In the fifth test case, one possible solution is a1=dokidokiliteraturecluba_1=dokidokiliteratureclub.


题目大意 看懂题意的话可以直接跳过

把一个长度为n的字符串,分成2k+12k+1份小字符串。其中第ii个小字符串与第2k+1i2k+1-i个字符串对称(abcabccbacba对称),问能不能做到。


解题思路 发现规律

首先长度为nn要分成2k+12k+1份,就要满足n2n+1n\ge2n+1。若不满足直接NONO

下面讨论满足n2n+1n\ge2n+1的情况:

其实找几个例子就能发现,ss由3部分组成:ABCA B C。其中

  1. AACC是对称的
  2. AABBCC都能为空
  3. A的长度kA的长度\ge k

样例分析

样例1

5 1
qwqwq

AA : qq BB : wqwwqw CC : qq 满足上述3个条件,故输出YES

样例2

2 1
ab

2<12+12<1*2+1,不满足条件3,故输出NO

样例3

3 1
ioi

AA : ii BB : oo CC : ii 满足上述3个条件,故输出YES

样例4

4 2
icpc

4<22+14<2*2+1,不满足条件3,故输出NO

样例5

22 0
dokidokiliteratureclub

AA : BB : dokidokiliteratureclubdokidokiliteratureclub CC : 满足上述3个条件,故输出YES

样例6

19 8
imteamshanghaialice

无法找到两个个长度8\ge 8AABB使得AABB对称,故输出NO

样例7

6 3
aaaaaa

AA : qq BB : wqwwqw CC : $$ 6<32+16<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;
}

同步发文于我的CSDN,原创不易,转载请附上原文链接哦~
Tisfy:blog.csdn.net/Tisfy/artic…