CF1496A Split it!(数学分析)

"本文探讨了在编程竞赛中如何利用字符串操作技巧,通过检查给定字符串是否能通过特定方式拆分成 k+1 个子串,满足反转和拼接条件。理解 R(a_i) 的定义并找出满足 s = a_1 + a_2 + ... + a_k + a_{k+1}
摘要由CSDN通过智能技术生成

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 a_1,a_2...,a_{k+1}a1​,a2​...,ak+1​ , such that 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) as a reversed string xx . For example R(abcd) = dcbaR(abcd)=dcba . Note that in the formula above the part R(a\_{k+1})R(a_k+1)$$ is intentionally skipped.

输入格式

The input consists of multiple test cases. The first line contains a single integer tt ( 1\le t\le 1001≤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 nn , kk ( 1\le n\le 1001≤n≤100 , 0\le k\le \lfloor \frac{n}{2} \rfloor0≤k≤⌊2n​⌋ ) — 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.

输出格式

For each test case, print "YES" (without quotes), if it is possible to find 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).

题意翻译

题目描述

给出一个长度为  n 的字符串 s ,再给定一个参数 k 。

问是否存在字符串 a1​,a2​,...,ak+1​,使得:

 s=a1​+a2​+...+ak​+ak+1​+R(ak​)+...+R(a2​)+R(a1​)

如果存在,输出 YES;否则输出 NO

上式中  + 为字符串拼接, R(ai​) 为  ai​ 的反串。

本题多测。

输入格式

第一行 t 表示数据组数。

对于每组数据:

第一行  n,k 如题所述,第二行一个字符串  s。

输出格式

共 t  行,第  i 行为第 i  组数据。

对于第 ii 组数据,如果 s  满足题意条件,输出 YES;否则输出 NO

数据范围

 1≤n≤100,0≤k≤⌊ n​/2⌋

输入输出样例

输入  

7
5 1
qwqwq
2 1
ab
3 1
ioi
4 2
icpc
22 0
dokidokiliteratureclub
19 8
imteamshanghaialice
6 3
aaaaaa

输出  

YES
NO
YES
NO
YES
NO
NO

题意分析,我们观察这几个案例规律,我们可以得到一个共同规律,就是当我们取第一位和最后一位是否相等然后用i,j分别控制加减达到2边遍历,我们发现相同的数目总是大于或者等于k;所以我们只需要用代码表示出来,具体操作看代码。

#include <bits/stdc++.h>
using namespace std;
 
int t, n, k;
char s[105];
 
int main() {
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%s", &n, &k, s);
        int cnt = 0;
        for (int i = 0, j = n - 1; i < j - 1; ++i, --j) {//结束条件是刚刚好i=j 
            if (s[i] != s[j]) {
                break;
            }
            ++cnt;//想不通就想qweaq为什么是YES;这就是数学规律 
        }
        puts(cnt >= k ? "YES" : "NO");
    }
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法编程张老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值