Vera and LCS
Time limit2sMemory limit256 MB
Given string A and target K, find the smallest i where A's prefix of length i plus N-i copies of A's least frequent letter has LCS exactly K with A.
- Level
Medium7 of 10
- Topics
- String, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
Vera is studying the longest common subsequence (LCS) problem.
A string is a sequence of lowercase letters and may be empty. A subsequence of a string is what is left after deleting some letters of , possibly none of them and possibly all of them. For example vra, a, the empty string, and vera are all subsequences of vera. A longest common subsequence of two strings and is a string that is a subsequence of both and is as long as possible. Two strings can have several longest common subsequences. Since ea is a longest common subsequence of vera and eats, the LCS length of that pair is 2.
Vera's homework gave her two strings and , both of length , and asked for the LCS length of and . She wrote the answer and then lost . You are given and . Find a string of length whose LCS length with is exactly . Vera may have made a mistake, in which case no such exists.
Input
The input has the following format.
N K
A
- and are integers
- consists of lowercase letters
Output
Several strings can have LCS length with , so only the string fixed by the following rule is accepted.
Let be the letter that occurs the fewest times in . If several letters tie, take the alphabetically smallest of them. A letter that never occurs in occurs 0 times. For let be the first letters of followed by copies of . Find the smallest such that the LCS length of and is exactly , and print on one line. If no such exists, print WRONGANSWER.
Notes
Whether a valid exists is not decided by and alone. It also depends on how many times each letter occurs in .