subject

Bessie has recently received a painting set, and she wants to paint the long fence at one end of her pasture. The fence consists of NN consecutive 1-meter segments (1≤N≤1051≤N≤105). Bessie has 26 different colors available, which she labels with the letters 'A' through 'Z' in increasing order of darkness ('A' is a very light color, and 'Z' is very dark). She can therefore describe the desired color she wants to paint each fence segment as a length-NN string where each character is a letter. Initially, all fence segments are uncolored. Bessie can color any contiguous range of segments with a single color in a single brush stroke as long as she never paints a lighter color over a darker color (she can only paint darker colors over lighter colors).
For example, an initially uncolored segment of length four can be colored as follows:
-> BBB. -> BBLL -> BQQL
Running short on time, Bessie thinks she may need to leave some consecutive range of fence segments unpainted! Currently, she is considering QQ candidate ranges (1≤Q≤1051≤Q≤105), each described by by two integers (a, b)(a, b) with 1≤a≤b≤N1≤a≤b≤N giving the indices of endpoints of the range a…ba…b of segments to be left unpainted.
For each candidate range, what is the minimum number of strokes needed to paint every fence segment outside those in the range with its desired color while leaving all fence segments inside the range uncolored? Note that Bessie does not actually do any painting during this process, so the answers for each candidate range are independent.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains NN and QQ.
The next line contains a string of length NN characters representing the desired color for each fence segment.
The next QQ lines each contain two space-separated integers aa and bb representing a candidate range to possibly leave unpainted.
OUTPUT FORMAT (print output to the terminal / stdout):
For each of the QQ candidates, output the answer on a new line.
SAMPLE INPUT:
8 2
ABBAABCB
3 6
1 4
SAMPLE OUTPUT:
4
3
Please use C++ as answer only!

ansver
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 02:00, ayoismeisjuam
The word ‘play’ comes with many different interpretations and a variety of definitions. discuss some of the various meanings tied to the word play. why is the concept of play thought to be an important addition to the workplace? do some (brief) research online and give an example of how play in the workplace is being done right.
Answers: 2
image
Computers and Technology, 22.06.2019 19:00, dadonelson2109
If your accelerator suddenly gets stuck what should you do
Answers: 2
image
Computers and Technology, 24.06.2019 15:50, GreatBaconGamer
Subscribe to j p g a m e t u b e on you tube ?
Answers: 2
image
Computers and Technology, 24.06.2019 22:30, rayniqueamee2002
When can you access the bios setup utility?
Answers: 1
You know the right answer?
Bessie has recently received a painting set, and she wants to paint the long fence at one end of her...

Questions in other subjects:

Konu
Mathematics, 24.09.2019 01:00
Konu
Mathematics, 24.09.2019 01:00