KSA String
InterviewTime limit1sMemory limit1024 MB
Given a string X, find the fewest deletions and front or end insertions that turn it into the KSA-periodic string of the same length.
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 with :
- 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.
You can perform the following operations on a string zero or more times.
- Remove one existing character.
- Add one arbitrary character at the front.
- Add one arbitrary character at the end.
Perform appropriate operations on the given string to turn it into a string of the same length as that the KSAAC staff like. Find the minimum number of operations needed.
Input
The first line contains the string .
Output
Print the minimum number of operations needed to turn into a string of the same length as that the KSAAC staff like.