#C2100. Make Every K Length Palindrome

    ID: 45380 Type: Default 1000ms 256MiB

Make Every K Length Palindrome

Make Every K Length Palindrome

Alice wants to check if her string s of length \(n\) is already in a form such that every contiguous substring of length \(k\) is a palindrome. A palindrome is a string that reads the same forwards and backwards. If every substring of length \(k\) is a palindrome then the string is valid and the output should be the string preceded by its length. Otherwise, output \(-1\).

More formally, for a string \(s\) and an integer \(k\), for every index \(i\) with \(1 \leq i \leq n - k + 1\), the substring \(s[i, i+k-1]\) must satisfy \[ s[i, i+k-1] = \text{reverse}(s[i, i+k-1]). \]

If \(k = 1\), every substring is trivially a palindrome.

You are given \(t\) test cases. For each test case, determine whether the string meets the condition and output accordingly:

  • If the string meets the condition, output two lines: the first line is the length of the string and the second line is the string itself.
  • If the string does not meet the condition, output a single line with \(-1\).

inputFormat

The first line contains an integer \(t\) \( (t \geq 3)\) indicating the number of test cases.

For each test case, the first line contains two space-separated integers \(n\) and \(k\), where \(n\) is the length of the string and \(k\) is the length of the substring to be checked.

The following line contains the string \(s\) consisting of lowercase English letters.

outputFormat

For each test case, if every contiguous substring of length \(k\) is a palindrome, output two lines: the first line is the length of the string and the second line is the string \(s\). Otherwise, output a single line with \(-1\).

The outputs for different test cases are printed consecutively.

## sample
6
5 3
ababa
4 2
abca
2 2
aa
3 1
qwe
4 4
abcd
5 5
abcba
5

ababa -1 2 aa 3 qwe -1 5 abcba

</p>