1496A - Split it - CodeForces Solution


brute force constructive algorithms greedy strings *900

Please click on ads to support us..

Python Code:

import os
import sys
from io import BytesIO, IOBase


def main():
    t = int(input())
    for _ in range(t):
        n, k = read_ints()
        s = input()
        if n < 2 * k + 1:
            print('NO')
        else:
            for i in range(k):
                if s[i] != s[n - i - 1]:
                    print('NO')
                    break
            else:
                    print('YES')



BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



def read_ints():
    return [int(c) for c in input().split()]


def print_int_lines(lst):
    print('\n'.join(map(str, lst)))


if __name__ == "__main__":
    main()

C++ Code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include<bitset>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<string>
#include<cstdlib>
#include<sstream>
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define test_case int t;cin>>t;while(t--)
typedef long long int ll;
typedef long double ld;
using namespace std;
void Abdo_Saad() { std::ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); }
ll gcd(ll a, ll b) { return(b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void set_bit(int& n, int idx) { n |= 1 << idx; }
void reset_bit(int& n, int idx) { n &= ~(1 << idx); }
void flib_bit(int& n, int idx) { n ^= 1 << idx; }
bool pal(string s)
{
	int n = s.size();
	for (int i = 0;i< n/2;i++)
	{
		if (s[i] != s[n - i - 1])return false;
	}
	return true;
}
void solve()
{
	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;
	if (!k)
	{
		cout << "YES\n";
		return;
	}
	if (2 * k == n)
	{
		cout << "NO\n";
		return;
	}
	string t1, t2;
	for (int i = 0;i < k;i++)
	{
		t1.push_back(s[i]);
		t2.push_back(s[n - i-1]);
	}
	reverse(all(t2));
	if (pal(t1 + t2))cout << "YES\n";
	else cout << "NO\n";
	
		
	
}
int main()
{
	Abdo_Saad();
	test_case
	solve();
}


Comments

Submit
0 Comments
More Questions

337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation