| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 2 초 | 2048 MB | 2 | 1 | 1 | 100.000% |
Chairman K is going to host a series of conferences over $N$ days. Each day, exactly one conference is held, and it takes place in one of the three venues: the main venue A or one of the sub-venues B and C.
The venue information for each conference is given as a string $S$ consisting of ‘A’, ‘B’, ‘C’, and ‘?’. For the $i$-th day (1ドル ≤ i ≤ N$), if the $i$-th character of $S$ is ‘A’, the conference is held in venue A. If it is ‘B’, it is held in venue B. If it is ‘C’, it is held in venue C. If it is ‘?’, the venue for the $i$-th day has not been decided yet. However, since the conferences on the first and $N$-th days are expected to have many participants, it has already been determined that venue A will be used on those days.
Chairman K now needs to assign a venue to each undecided conference, choosing one of A, B, or C for each. Additionally, in order to minimize the burden of moving between venues, he wants to minimize the number of indices $j$ (1ドル ≤ j ≤ N - 1$) such that the venue for the $j$-th day differs from the venue for the $(j + 1)$-th day.
There are $Q$ scenarios to consider regarding the assignment of venues. The $k$-th scenario (1ドル ≤ k ≤ Q$) and the corresponding question are as follows:
Given the information about the venues and scenarios to consider, write a program to answer the questions.
Read the following data from the standard input.
$N$
$S$
$Q$
$X_1$ $Y_1$ $Z_1$
$X_2$ $Y_2$ $Z_2$
$\vdots$
$X_Q$ $Y_Q$ $Z_Q$
Write $Q$ lines to the standard output. In the $k$-th line (1ドル ≤ k ≤ Q$), output the minimum number of indices $j$ such that the venue for the $j$-th day differs from the venue for the $(j + 1)$-th day, under the condition that Chairman K assigns $X_k$ undecided conferences to venue A, $Y_k$ to venue B, and $Z_k$ to venue C.
A’, ‘B’, ‘C’, and ‘?’.A’.?’ in $S$ (1ドル ≤ k ≤ Q$).| 번호 | 배점 | 제한 |
|---|---|---|
| 1 | 4 | $N ≤ 50$ and the number of ‘ |
| 2 | 7 | $N ≤ 500$. |
| 3 | 13 | $N ≤ 5,円 000,ドル $Q ≤ 10$. |
| 4 | 18 | $N ≤ 5,円 000$. |
| 5 | 12 | $Q ≤ 10$. |
| 6 | 8 | $S$ does not contain ‘ |
| 7 | 13 | $Z_k = 0$ (1ドル ≤ k ≤ Q$). |
| 8 | 25 | No additional constraints. |
9 A??B??C?A 3 1 3 1 4 1 0 0 0 5
3 4 4
In the first scenario, Chairman K has to assign 1ドル$ of the 5ドル$ undecided conferences to venue A, 3ドル$ to venue B, and 1ドル$ to venue C. For example, one possible assignment results in the venue information string “ABBBBCCAA”. In this case, the indices $j$ such that the venue for the $j$-th day differs from the venue for the $(j + 1)$-th day are 1ドル,ドル 5ドル,ドル 7ドル,ドル which means there are 3ドル$ such indices. Since it is impossible to reduce this number to 2ドル$ or fewer, the correct output for the first line is 3ドル$.
In the second scenario, Chairman K has to assign 4ドル$ of the 5ドル$ undecided conferences to venue A and 1ドル$ to venue B. For example, one possible assignment results in the venue information string “AAABBACAA”. In this case, the indices $j$ such that the venue for the $j$-th day differs from the venue for the $(j + 1)$-th day are 3ドル,ドル 5ドル,ドル 6ドル,ドル 7ドル,ドル which means there are 4ドル$ such indices. Since it is impossible to reduce this number to 3ドル$ or fewer, the correct output for the second line is 4ドル$.
In the third scenario, Chairman K has to assign all 5ドル$ undecided conferences to venue C. The indices $j$ such that the venue for the $j$-th day differs from the venue for the $(j + 1)$-th day are 1ドル,ドル 3ドル,ドル 4ドル,ドル 8ドル,ドル which means there are 4ドル$ such indices. Therefore, the correct output for the third line is 4ドル$.
This sample input satisfies the constraints of subtasks 1, 2, 3, 4, 5, and 8.
12 A???A?B????A 4 0 8 0 2 6 0 7 1 0 3 5 0
4 4 2 2
This sample input satisfies the constraints of all the subtasks.
28 ACB??B???BCB??B????B?AAA?BBA 26 6 1 6 4 5 4 2 3 8 9 2 2 11 0 2 8 4 1 11 0 2 2 0 11 0 1 12 12 1 0 10 3 0 1 4 8 3 7 3 2 8 3 1 3 9 11 1 1 7 0 6 6 4 3 8 4 1 0 10 3 13 0 0 11 1 1 0 6 7 2 8 3 9 0 4 0 0 13
15 11 13 13 15 12 15 15 16 15 13 12 10 9 13 15 15 11 12 9 15 15 11 9 15 17
This sample input satisfies the constraints of subtasks 1, 2, 4, and 8.