TopCoder problem "ReverseResources" used in SRM 342 (Division I Level Two)

Problem Statement


You are working on a large software project and there is a broad assortment of warning or error messages that your users might see in the course of using your product.

In order to support localization (translation of these messages into other languages), all of the strings used to create error and warning messages are stored in a resource library. The actual message might be constructed out of more than one resource string. The way this is implemented (for the sake of this problem) is that within a resource string, any instance of "%s" is replaced with another resource string. That other resource string might also have a "%s" in it that is replaced with yet another resource string. For example, if you have resource strings {"I am %s.", "Bob", "a %s coder", "good"}, you could replace the "%s" in "I am %s." with "Bob", and you'd have the output string "I am Bob.". Alternatively, you could replace the "%s" in "a %s coder" with "good", and you'd end up with "a good coder". Then you could replace the "%s" in "I am %s." with "a good coder", and you'd end up with "I am a good coder."

Constructing these strings is the easy part. Often when customers have problems, they only know the last error message they saw. In order to troubleshoot their problems, you need to know what resource was used to construct an error message. Sometimes, there might be more than one possibility. Given a message str and the set of resource strings, resources, return the number of ways str might have been constructed from the elements in resources, modulo 1000000007. If there is more than one element in resources that is the same, each instance should be considered distinct (see example 8 for clarification).



Parameters:String, String[]
Method signature:int findDecompositions(String str, String[] resources)
(be sure your method is public)


-All instances of "%s" must be replaced with another string, because str will have no '%' characters in it.


-str will contain between 1 and 30 characters, inclusive.
-resources will contain between 1 and 50 elements, inclusive.
-Each element of resources will have contain between 1 and 50 characters, inclusive.
-Each element of resources will contain only letters ('A'-'Z' or 'a'-'z'), digits ('0'-'9'), and '%', '.', ',', '!' and spaces (' ').
-str will contain only letters ('A'-'Z' or 'a'-'z'), digits ('0'-'9'), '.', ',', '!' and spaces (' ').
-Each occurrence of '%' in an element of resources will be followed by an 's'.
-None of the elements of resources will be just "%s".


"Error in module foo, code 123."
{"foo", "123", "Error in module %s.", "%s, code %s"}
Returns: 1
This string's decomposition is fairly obvious.
"The fox jumped over the dog."
{"The fox %s over the dog.",
 "The %s",
 "fox %s",
 "%s over %s",
 "the dog."}
Returns: 5
Using parenthesized expressions to represent substitutions, the five ways to decompose this string are:
  • The fox (jump(ed)) over the dog.
  • The (fox ((jump(ed)) over (the dog.)))
  • The ((fox (jump(ed))) over (the dog.))
  • (The (fox (jump(ed)))) over (the dog.)
  • The (fox (jump((ed) over (the dog.))))
{"%sc%s", "b", "de", "a%s"}
Returns: 2
Decompositions this time are:
  • (a(b))c(de)
  • a((b)c(de))
{"%se", "a%s", "%sb%s", "%sc%s", "%sd%s"}
Returns: 0
If all the strings have %s in them, there are never any valid decompositions!
Returns: 14
Returns: 879704799
{"a", "a", "%s%s", "%s%s"}
Returns: 8
The elements in resources may not be distinct, but should be considered distinct. In this example, either "%s%s" could be used and either or both "a"s could be used, in either order.

Problem url:

Problem stats url:




PabloGilberto , brett1479 , , Olexiy

Problem categories:

Dynamic Programming, String Parsing