#AGC059A. [AGC059A] My Last ABC Problem
[AGC059A] My Last ABC Problem
Score : points
Problem Statement
Consider a string consisting only of characters A, B, and C.
We can do the following operation with it:
- Choose any substring and any permutation of characters
ABC. Here, denotes the substring formed by the -th through the -th characters of , where and are of your choice. Then, replace each characterA,B, andCin by , , and , respectively.
For example, for a string ACBAAC, we can choose a substring and CBA.
After this operation, the string will become ACBCCA.
Alina likes strings in which all characters are the same. She defines the beauty of a string as the minimum number of operations required to make all its characters equal.
You are given a string of length consisting only of characters A, B, and C.
Answer queries. The -th query is the following:
- Given integers and , find the beauty of the substring .
Constraints
- is a string of length consisting only of characters
A,B, andC. - All numbers in the input are integers.
Input
Input is given from Standard Input in the following format:
Output
Output lines. In the -th line, output the answer to the -th query.
6 4
ABCCCA
3 5
2 3
1 3
1 6
0
1
2
2
In the first query, the string is CCC, in which all letters are already equal. The answer is .
In the second query, the string is BC. We can change it to BB in one operation, by choosing a substring and ACB.
In the third query, the string is ABC. We can change it to AAB in one operation, by choosing a substring and CAB, and then to BBB in the next operation, by choosing a substring and BAC.