Conference

시간 제한2초메모리 제한2048 MB

요약
각 날짜의 공연장 정보가 A, B, C, ?로 주어지고, 물음표를 A, B, C로 각각 몇 개씩 배정하는 질의마다 이웃한 날의 공연장이 달라지는 횟수의 최솟값을 구한다.
난이도

어려움10점 중 8점

유형
그리디, 동적 계획법, 누적 합, 구현
정답자
아직 제출이 없습니다

문제

Chairman K is going to host a series of conferences over NN 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 SS consisting of ‘A’, ‘B’, ‘C’, and ‘?’. For the ii-th day (1≤i≤N1 ≤ i ≤ N), if the ii-th character of SS 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 ii-th day has not been decided yet. However, since the conferences on the first and NN-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 jj (1≤j≤N−11 ≤ j ≤ N - 1) such that the venue for the jj-th day differs from the venue for the (j+1)(j + 1)-th day.

There are QQ scenarios to consider regarding the assignment of venues. The kk-th scenario (1≤k≤Q1 ≤ k ≤ Q) and the corresponding question are as follows:

  • Chairman K has to assign X_kX\_k undecided conferences to venue A, Y_kY\_k to venue B, and Z_kZ\_k to venue C. Determine the minimum possible number of indices jj such that the venue for the jj-th day differs from the venue for the (j+1)(j + 1)-th day.

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.

NN

SS

QQ

X_1X\_1 Y_1Y\_1 Z_1Z\_1

X_2X\_2 Y_2Y\_2 Z_2Z\_2

⋮\vdots

X_QX\_Q Y_QY\_Q Z_QZ\_Q

출력

Write QQ lines to the standard output. In the kk-th line (1≤k≤Q1 ≤ k ≤ Q), output the minimum number of indices jj such that the venue for the jj-th day differs from the venue for the (j+1)(j + 1)-th day, under the condition that Chairman K assigns X_kX\_k undecided conferences to venue A, Y_kY\_k to venue B, and Z_kZ\_k to venue C.

제한

  • 2≤N≤300,0002 ≤ N ≤ 300\\, 000.
  • SS is a string of length NN consisting of ‘A’, ‘B’, ‘C’, and ‘?’.
  • The first and NN-th characters of SS are ‘A’.
  • 1≤Q≤200,0001 ≤ Q ≤ 200\\, 000.
  • 0≤X_k0 ≤ X\_k (1≤k≤Q1 ≤ k ≤ Q).
  • 0≤Y_k0 ≤ Y\_k (1≤k≤Q1 ≤ k ≤ Q).
  • 0≤Z_k0 ≤ Z\_k (1≤k≤Q1 ≤ k ≤ Q).
  • X_k+Y_k+Z_kX\_k + Y\_k + Z\_k is equal to the number of ‘?’ in SS (1≤k≤Q1 ≤ k ≤ Q).
  • NN, QQ, X_kX\_k, Y_kY\_k, Z_kZ\_k are all integers.

예제3

  1. 예제 1

    입력
    9
    A??B??C?A
    3
    1 3 1
    4 1 0
    0 0 5
    
    예상 출력
    3
    4
    4
    
  2. 예제 2

    입력
    12
    A???A?B????A
    4
    0 8 0
    2 6 0
    7 1 0
    3 5 0
    
    예상 출력
    4
    4
    2
    2
    
  3. 예제 3

    입력
    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