TopCoder problem "TaylorAlgebra" used in TCO06 Semi 3 (Division I Level One)



Problem Statement

    You will be given a String formula that adheres to the folowing pseudo-grammar (quotes for clarity):
  <FORMULA> :== D(<FORMULA>) | I(<FORMULA>) | T(<NUM>,<FORMULA>) | 'f'
      <NUM> :== a positive integer between 1 and 100 inclusive with no leading zeros
The following congruences allow us to change a formula into an equivalent one (here F denotes some <FORMULA>):
      D(I(F)) == F
      I(D(F)) == F
    D(T(i,F)) == T(i-1,D(F))
    I(T(i,F)) == T(i+1,I(F))
Return a String describing a formula equivalent to the input such that the total number of 'D's and 'I's is minimized. The returned value must be formatted like the input. If there are multiple possible return values, choose the one where no 'D' or 'I' could be nested deeper using a single congruence. Remember that any number in the returned value, as well as in any intermediate formulae, must be between 1 and 100, inclusive.
 

Definition

    
Class:TaylorAlgebra
Method:getCanonical
Parameters:String
Returns:String
Method signature:String getCanonical(String formula)
(be sure your method is public)
    
 

Constraints

-formula will contain between 1 and 50 characters, inclusive.
-formula will adhere to the grammar in the statement.
 

Examples

0)
    
"T(100,f)"
Returns: "T(100,f)"
None of the congruences are appropriate.
1)
    
"D(T(50,f))"
Returns: "T(49,D(f))"
The 'D' can be nested more deeply.
2)
    
"D(T(1,f))"
Returns: "D(T(1,f))"
We want to push the 'D' inward, but that would drop the number lower than 1.
3)
    
"I(T(100,f))"
Returns: "I(T(100,f))"
Pushing the 'I' inward would force the number above 100.
4)
    
"D(T(40,I(f)))"
Returns: "T(39,f)"
5)
    
"I(T(40,D(f)))"
Returns: "T(41,f)"

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6235

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=9982&pm=6235

Writer:

AdminBrett

Testers:

PabloGilberto , lbackstrom , vorthys , Olexiy

Problem categories:

String Parsing