아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Chocolate is Tasty

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

요약
n x m 초콜릿과 원형으로 앉은 아이들이 주어지고, 욕심 많은 남자아이는 긴 변에서, 몸매를 관리하는 여자아이는 짧은 변에서 한 줄씩 먹는다. 서로 다른 아이가 최대로 많이 먹도록 시작 위치를 정한다.
난이도

보통10점 중 6점

유형
구현, 시뮬레이션, 수학, 그리디
정답자
아직 제출이 없습니다

문제

A mother wishes to share a chocolate bar among her children. A child can be either a boy or a girl. The children sit in a circle and consecutively take a line of chocolate in clockwise order. At the beginning, the chocolate bar has nn rows and mm columns. Each boy is greedy and takes one line from the longer side of the chocolate bar. Each girl wishes to keep fit so she takes one line from the shorter side. Mother has to choose from which child she should start to maximize the number of kids that will share the chocolate.

입력

The first line of input consists of zz, the number of test cases. The description of the test cases follows.

Each line consists of two integers nn and mm separated by a space (1≤n,m≤1061 \leq n, m \leq 10^6). They are followed by a space and a sequence of letters "B" and "G" which identify the children on the circle in clockwise order. The length of the sequence is not greater than 10610^6.

출력

For each test case, print a line with single a interger: the maximal number of children that can eat a chocolate. Do not count the same kid twice even if he/she receive the chocolate twice.

예제1

  1. 예제 1

    입력
    3
    4 4 GGGGGGG
    4 4 BBBBBBB
    4 4 GBGBBBG
    
    예상 출력
    7
    4
    7