KSA String 2
Time limit1sMemory limit1024 MB
Given a string of K, S, and A, find the minimum number of remove or move-to-front operations that turn it into the longest string repeating KSA.
- Level
Medium7 of 10
- Topics
- String, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
The KSAAC staff all love KSA, so they like strings that satisfy the following condition.
Let the length of the string be . For every :
- If the remainder of divided by is , the -th character is
K. - If the remainder of divided by is , the -th character is
S. - If the remainder of divided by is , the -th character is
A.
The empty string is also a string that the KSAAC staff like.
You may perform the following operations or more times. Each time, you choose one of the two options below.
- Remove any one existing character.
- Move any one existing character to the front.
Perform appropriate operations on the given string to turn into a liked string of the maximum possible length. Find the minimum number of operations required.
Input
The first line contains the string .
Output
Print the minimum number of operations needed to turn the string into a liked string of the maximum possible length.