#P13961. [ICPC 2023 Nanjing R] 华丽收场
[ICPC 2023 Nanjing R] 华丽收场
Description
In Pigeland, is the most popular roguelike game where players use card decks to challenge a boss called Evil Potato Lord.
The general game rules are as follows:
- Players start with a set of initial cards in their hand and a draw pile arranged from top to bottom.
- At any given time, the player's hand and the draw pile are the only places where cards are present.
- Players can play cards from their hands, and each played card will lead to this card being discarded first, and then triggering certain effects.
- The player can only use the next card after all the effects of the previously played card have been triggered.
In this problem, for simplicity, we will only consider the effect of drawing cards. The rules for drawing cards are as follows:
- When using cards that can draw some other cards, players will draw a certain number of cards from the top of the draw pile into their hands in sequential order.
- Players have a hand size limit of , and at any given moment, the number of cards in a player's hand cannot exceed the limitation .
- When a player attempts to draw a card from the top of the draw pile and their hand already contains cards, the drawn card will be discarded from the draw pile without triggering any effects and not added to the hand.
- When a player attempts to draw a card but the draw pile is empty, nothing will happen.
Decks based on the key card are the most powerful deck in the game. Once this card is played, it will cause massive damage to all enemies and often leads to victory in the game. However, to play the Grand Finale card, there are strict conditions. That is, the player's draw pile must be empty, which means that all cards must either be played, discarded, or in the player's hand.
Bie-Bot is the smartest pig in Pigeland only after the Mysterious Oscar, and he is also playing a -based deck, which can contain the following four types of cards:
:::align{center}
:::
- : The most powerful card in the game, playing it will lead to an easy victory, and there is exactly one in Bie-Bot's deck. This card can only be played if there are no cards in the draw pile.
- : Using this card, you will draw one card from the draw pile.
- : Using this card, you will draw two cards from the draw pile.
- : A status card that, once it is in your hand, can not be played.
At the beginning of the game, Bie-Bot was lucky to have drawn the only one in his starting hand, and he also knows that each card in the draw pile from top to bottom. Now, his goal is to successfully play the card. Bie-Bot wants to know, under his optimal strategy, what is the minimum hand size limit required for him to achieve his goal. As the third smartest player in Pigeland, can you help him out?
More formally, you are given a string of length representing Bie-Bot's starting hand and a string of length representing the draw pile from top to bottom. Both strings consist of uppercase letters G, Q, B and W, indicating that the card at the corresponding position in the starting hand or draw pile is , , , or , respectively. Bie-Bot can use the cards in his hand according to the rules mentioned above. Please output the minimum hand size limit () such that Bie-Bot can finally play the or state that there is no such .
Input Format
There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:
The first line contains two integers and () indicating the number of cards in Bie-Bot's starting hand and draw pile.
The second line contains a string of length to represent Bie-Bot's starting hand, consisting of uppercase letters G, Q, B, and W. It's guaranteed that there is exactly one G in .
The third line of the input contains a string of length to represent Bie-Bot's draw pile from top to bottom, consisting of uppercase letters Q, B, and W.
It is guaranteed that the sum of of all test cases will not exceed .
Output Format
For each test case output one line containing one integer indicating the minimum hand size () that can lead to successfully playing , or if it can't be played.
2
2 6
BG
BQWBWW
4 6
GQBW
WWWWQB
3
IMPOSSIBLE
Hint
We express the situation with "hand/deck" string. For the first sample test case, one optimal strategy is:
- BG/BQWBWW BQG/WBWW
- BQG/WBWW BWG/BWW
- BWG/BWW BWG/W (Discard one "W" here due to hand size limit)
- BWG/W WWG/ (Only draw one card here because there is no more card in the draw pile)
- WWG/ Successfully playing !
京公网安备 11011102002149号