TopCoder problem "PresentationComp" used in SRM 221 (Division I Level Three)



Problem Statement

    In algebra, a presentation is a convenient way to describe a set. The presentation includes what the atomic elements of the set are, and what relations are used to simplify strings of atoms. These atoms are usually called generators. In this problem we will be looking at a set whose generators are x and y. You will be given a String expression consisting of x's and y's. The simplifying rules are:
  • 1) Any occurrence of yyyyyy can be deleted from the string.
  • 2) Any occurrence of xxxxxxxx can be deleted from the string.
  • 3) Any occurrence of xy can be replaced by yyyyyx.
Two strings A and B are equivalent if there is a string C such that A can be simplified into C and B can be simplified into C by applying the rules above 0 or more times. Return the shortest string equivalent to expression. If there are multiple possible solutions, return the one that comes first alphabetically.
 

Definition

    
Class:PresentationComp
Method:simplify
Parameters:String
Returns:String
Method signature:String simplify(String expression)
(be sure your method is public)
    
 

Constraints

-expression will contain between 1 and 50 characters inclusive.
-Each character in expression will be x or y.
 

Examples

0)
    
"xxxxxxxxyyyyyy"
Returns: ""
Simplifies greatly.
1)
    
"xy"
Returns: "xy"
Doesn't get much simpler.
2)
    
"yyxx"
Returns: "xxyy"
Use the one that comes first alphabetically.

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5867&pm=2332

Writer:

AdminBrett

Testers:

PabloGilberto , lbackstrom

Problem categories:

Encryption/Compression, Math, String Manipulation