Elections

C와 T로 이루어진 투표 문자열의 각 부분 구간에서, 남은 투표를 왼쪽에서 오른쪽으로, 그리고 오른쪽에서 왼쪽으로 셀 때 C가 T에게 한 번도 뒤지지 않도록 지워야 하는 최소 투표 수를 구한다.

어려움8그리디누적 합동적 계획법구간아직 제출이 없습니다시간 제한2초메모리 제한256 MB

문제

Today we have to decide which is the best superhero team: Team Cap or Team Iron Man (Tony)? There are N enraged fans, each of which has to vote for his favorite superhero team: either ‘C’ (standing for “Cap”) or “T” (standing for “Tony”).

Since Cap knows he has no chance to win, and since he is a very honest guy, he decided to commit election fraud. He wants to nullify a minimum number of votes.

The vote counting is done twice:

  • in increasing order of the fans’ indices, excluding those with a nullified vote
  • in decreasing order of the fans’ indices, excluding those with a nullified vote

Cap is happy if at every point in time during the vote counting he is NOT losing to Tony (he doesn’t need to have more votes at any time, but he should never have less votes).

Of course, no one can mess with Tony. He knows Cap’s plan and he is interested for Q scenarios what is the number of votes Cap is going to nullify. A scenario is defined by 2 numbers L and R signifying that only the fans with indices from L to R, inclusively, are going to participate in the voting session.

입력

The first line contains a single number N, the number of fans.

The second line contains a string of N characters from the set {‘C’, ‘T’}, the votes of each fan.

The third line contains a single number Q, the number of scenarios.

The next Q lines describe the Q scenarios in the form L R (1 ≤ L ≤ R ≤ N).

출력

Print Q lines, the i-th of which contains the result for the i-th scenario.

힌트

In the first scenario, the votes look like this: "CCCTTTTTTCC". The only solution is to nullify 4 of Tony’s votes.

In the second scenario, the votes look like this: "TTTTTT". The only solution is to nullify all votes.

In the last scenario, the votes look like this: "CCCTTT". The only solution is to nullify all of Tony’s votes.