#C1670. Construct the Longest Palindrome
Construct the Longest Palindrome
Construct the Longest Palindrome
You are given an integer k
and a string s
consisting of lowercase letters only. Your task is to remove characters from s
(without reordering the remaining characters) in order to obtain a palindromic string. However, you are allowed to remove at most k
characters during this process.
Your goal is to output the longest palindromic string that can be created under these rules. If there are several possible answers, any one of them is acceptable. Note that even if the minimum number of deletions required to make the string fully palindromic exceeds k
, you should still form the longest palindrome possible by judiciously choosing deletions.
Examples:
- For
k = 2
ands = "abcbda"
, one possible answer isabcba
. - For
k = 1
ands = "abcde"
, one possible answer isa
. - For
k = 3
ands = "banana"
, one possible answer isanana
.
inputFormat
The input is read from standard input (stdin
) and consists of two lines:
- The first line contains a single integer
k
which is the maximum number of characters you are allowed to remove. - The second line contains the string
s
consisting of only lowercase letters.
outputFormat
Print to standard output (stdout
) the longest palindromic string that can be formed from s
by removing at most k
characters.
2
abcbda
abcba