TopCoder problem "Macros" used in TCCC '03 Round 2 (Division I Level Three)

Problem Statement

    You have been working with a macro processor that has rules of the following form:
   VarChar : VarOrTextChar VarOrTextChar
   where VarChar is an uppercase letter, 
   and VarOrTextChar is an uppercase or lowercase letter
The macro processor only recognizes 2 kinds of characters, namely Variable Characters (uppercase letters) and Text Characters (lowercase letters). If it finds a Variable Character in the text that matches the left side of one of its rules it can replace it with the characters on the right side of that rule. If more than one rule has a left side that matches, the processor can arbitrarily choose which rule to use. This process continues until the processor gets rid of all the Variable Characters. For example, let's say the processor had the following rules:
rules = {"S:aA",
Then given the string "S" it could do the following actions:

S => replace S with aA => aA => replace A with Sb => aSb => replace S with ab => aabb

or perhaps

S => replace S with ab => ab.

These are just two possible sequences of actions out of an infinite number of possible sequences that the processor could follow with the above rules. Our problem is, we have only been given the output and are clueless as to the initial input. Given the rules of the processor, and the output text, your method will return all of the Variable Characters that could have produced the given output, in lexicographic ('A' to 'Z') order. Using the rules in the above example, if given output "aabb", your method would return {"D","S"}.


Parameters:String[], String
Method signature:String[] whichOnes(String[] rules, String output)
(be sure your method is public)


-rules will contain between 1 and 50 elements, inclusive.
-Each element of rules will have 4 characters.
-The first character of each element of rules will be an uppercase letter ('A'-'Z').
-The second character of each element of rules will be a colon (':').
-The third and fourth characters of each element of rules will be uppercase or lowercase letters ('A'-'Z','a'-'z').
-output will have between 2 and 50 characters, inclusive.
-output will contain only lowercase letters ('a'-'z').


Returns: { "D",  "S" }
This is the example from the problem statement.
Returns: { "S" }
The only way to produce "ab" is to start with "S" and immediately replace "S" with "ab", using the third rule.
Returns: { }
Returns: { }
Returns: { "A" }
Returns: { "A",  "B",  "C" }

Problem url:

Problem stats url:




lbackstrom , schveiguy

Problem categories:

Dynamic Programming