Words
Time limit1sMemory limit128 MB
Given a word of length n, find the smallest number of blocks in a word that differs from it in at most k positions.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String
- Solved
- No attempts yet
Problem
A word is a sequence of capital letters of the English alphabet. The length of a word is the number of letters it contains. For example, the word = ABAACBBBA has length .
A block of a word is a maximal run of identical letters. A word is -hard if it consists of exactly blocks. The word above is -hard, because it splits into the blocks A | B | AA | C | BBB | A.
Two words of the same length can be compared by how much they differ. Two words of length are -different if they differ in exactly positions (with ): the -th letter of the first word differs from the -th letter of the second. For example, and = AAAABBBBB are -different.
Given a word , we want a word that is not too different from yet as simple as possible, and we ask how simple can be.
Write a program that reads , , and a word of length , and finds the smallest such that there exists a -hard word differing from in at most positions. Output this value of .
Input
The first line contains two integers and separated by a single space (, ): the length of the word and the allowed number of differing positions. The second line contains exactly capital letters forming the word .
Output
Output a single integer: the minimum value of .