Approach
Depth-first search
For CCC 2008 J4 - From Prefix to Postfix, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 68 lines of Turing from the credited upstream file ccc08j4.t.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123456789101112131415161718192021222324252627282930313233 34 35var prefix : string36var post1, temp : string37 38procedure postfix (s : string, var post1 : string, var rest : string)39 var first, second, temp1, temp2 : string40 put "in:" + s41 if s (1) = '+' then42 postfix (s (3 .. *), first, temp1)43 postfix (temp1, second, temp2)44 post1 := first + " " + second + " +"45 rest := temp246 elsif s (1) = '-' then47 postfix (s (3 .. *), first, temp1)48 postfix (temp1, second, temp2)49 post1 := first + " " + second + " -"50 rest := temp251 else52 post1 := s (1)53 if length (s) > 1 then54 rest := s (3 .. *)55 else56 rest := ""57 end if58 end if59 put "out:" + post1 + ":" + rest60end postfix61 62loop63 get prefix : *64 exit when prefix = "0"65 postfix (prefix, post1, temp)66 put post167end loop68