Shift the letters

3.7

30 votes
Algorithms, Dynamic Programming, Easy, Two dimensional, 簡単-普通
Problem

You are given a string S that contains lowercase English letters. In one step, you can choose an index i from S and assign the next letter in the alphabet (ab,bc,,za) to Si. Define f(str) as the number of indices i (1i<|str|) where stristri+1.

You are given a number K. Now, your task is to determine the minimum number of steps required to perform on the string S to obtain a string S such that f(S)K.

Input format

  • First line: String S (1|S|2048)
  • Second line: Integer K (0K<min(|S|,32))

Output format

Print the minimum number of steps.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

If you set S1 and S2 to cby doing 3 moves, you obtain S="cccz" for which f(S)=1.

Editor Image

?