#P13362. [GCJ 2011 Qualification] Magicka

[GCJ 2011 Qualification] Magicka

题目背景

Magicka™ is an action-adventure game developed by Arrowhead Game Studios. In Magicka you play a wizard, invoking and combining elements to create Magicks. This problem has a similar idea, but it does not assume that you have played Magicka.

Note: "invoke" means "call on." For this problem, it is a technical term and you don't need to know its normal English meaning.

Magicka™ is a trademark of Paradox Interactive AB. Paradox Interactive AB does not endorse and has no involvement with Google Code Jam.

题目描述

As a wizard, you can invoke eight elements, which are the "base" elements. Each base element is a single character from {Q,W,E,R,A,S,D,F}\{Q, W, E, R, A, S, D, F\}. When you invoke an element, it gets appended to your element list. For example: if you invoke WW and then invoke AA, (we'll call that "invoking WAWA" for short) then your element list will be [W,A][W, A].

We will specify pairs of base elements that combine to form non-base elements (the other 18 capital letters). For example, QQ and FF might combine to form TT. If the two elements from a pair appear at the end of the element list, then both elements of the pair will be immediately removed, and they will be replaced by the element they form. In the example above, if the element list looks like [A,Q,F][A, Q, F] or [A,F,Q][A, F, Q] at any point, it will become [A,T][A, T].

We will specify pairs of base elements that are opposed to each other. After you invoke an element, if it isn't immediately combined to form another element, and it is opposed to something in your element list, then your whole element list will be cleared.

For example, suppose QQ and FF combine to make TT. RR and FF are opposed to each other. Then invoking the following things (in order, from left to right) will have the following results:

  • QF[T]QF \rightarrow [T] (QQ and FF combine to form TT)
  • QEF[Q,E,F]QEF \rightarrow [Q, E, F] (QQ and FF can't combine because they were never at the end of the element list together)
  • RFE[E]RFE \rightarrow [E] (FF and RR are opposed, so the list is cleared; then EE is invoked)
  • REF[]REF \rightarrow [] (FF and RR are opposed, so the list is cleared)
  • RQF[R,T]RQF \rightarrow [R, T] (QFQF combine to make TT, so the list is not cleared)
  • RFQ[Q]RFQ \rightarrow [Q] (FF and RR are opposed, so the list is cleared)

Given a list of elements to invoke, what will be in the element list when you're done?

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case consists of a single line, containing the following space-separated elements in order:

First an integer CC, followed by CC strings, each containing three characters: two base elements followed by a non-base element. This indicates that the two base elements combine to form the non-base element. Next will come an integer DD, followed by DD strings, each containing two characters: two base elements that are opposed to each other. Finally there will be an integer NN, followed by a single string containing NN characters: the series of base elements you are to invoke. You will invoke them in the order they appear in the string (leftmost character first, and so on), one at a time.

输出格式

For each test case, output one line containing "Case #xx: yy", where xx is the case number (starting from 1) and yy is a list in the format "[e0,e1,][e_0, e_1, \dots]" where eie_i is the ithi^{th} element of the final element list. Please see the sample output for examples.

5
0 0 2 EA
1 QRI 0 4 RRQR
1 QFT 1 QF 7 FAQFDFQ
1 EEZ 1 QE 7 QEEEERA
0 1 QW 2 QW
Case #1: [E, A]
Case #2: [R, I, R]
Case #3: [F, D, T]
Case #4: [Z, E, R, A]
Case #5: []

提示

Limits

  • 1T1001 \leq T \leq 100.
  • Each pair of base elements may only appear together in one combination, though they may appear in a combination and also be opposed to each other.
  • No base element may be opposed to itself.
  • Unlike in the computer game Magicka, there is no limit to the length of the element list.

Small dataset (10 Pts, Test set 1 - Visible)

  • 0C10 \leq C \leq 1.
  • 0D10 \leq D \leq 1.
  • 1N101 \leq N \leq 10.
  • Time limit: 30 3 seconds.

Large dataset (15 Pts, Test set 2 - Hidden)

  • 0C360 \leq C \leq 36.
  • 0D280 \leq D \leq 28.
  • 1N1001 \leq N \leq 100.
  • Time limit: 60 6 seconds.