TopCoder problem "InfiniteAddition" used in SRM 385 (Division I Level Three)



Problem Statement

    

An infinite periodic string is a string consisting of a non-periodic string S repeated infinitely, and is denoted as (S). For example, (ABC)=ABCABCABCA... Note that S must not be a repetition itself, so (ABAB)=ABABABAB... is not legal, and should be written as (AB)=ABABABAB... instead. S must contain only uppercase letters ('A'-'Z').

The sum of two infinite periodic strings is the string where each character is the sum of the corresponding characters in the two strings. Two characters are added as follows. Adding A to a character means changing it to the next character in the alphabet (where A is the next character after Z): A+A=B, A+B=C, A+C=D, ..., A+Y=Z, A+Z=A. Adding B to a character means changing it to the next character twice: B+A=C, B+B=D, B+C=E, ..., B+Z=B. All the other characters behave in the same manner (C means changing to the next character 3 times, D means 4, ..., Z means 26, so adding Z doesn't change a character).

The sum of two infinite periodic strings is itself an infinite periodic string. For example, (YAAZZB)=(AB)+(XYZ).

Given three integers, sum, op1 and op2, return an example of such an addition formatted as "(S)=(P)+(Q)" (quotes for clarity only), where S contains exactly sum characters, P contains exactly op1 characters, and Q contains exactly op2 characters. Remember that S, P and Q each need to be non-periodic.

If there are several possible answers, return the one among them where S comes earliest alphabetically. If there is a tie, return the answer among the ties where P comes earliest alphabetically. If there is no solution, return "NO SOLUTION" (quotes for clarity only).

 

Definition

    
Class:InfiniteAddition
Method:giveExample
Parameters:int, int, int
Returns:String
Method signature:String giveExample(int sum, int op1, int op2)
(be sure your method is public)
    
 

Constraints

-sum, op1 and op2 will each be between 1 and 20, inclusive.
 

Examples

0)
    
6
2
3
Returns: "(AAABZB)=(AB)+(ZYZ)"
The more 'A's you have in the beginning, the better. However, you can't make it just "(AAAAAA)=(AA)+(ZZZ)", as the strings inside parentheses need to be non-periodic.
1)
    
10
5
5
Returns: "NO SOLUTION"
No matter what the added strings are, the period of the sum can't be more than 5 in this case.
2)
    
5
5
5
Returns: "(AAAAB)=(AAAAC)+(ZZZZY)"
3)
    
1
5
5
Returns: "(A)=(AAAAB)+(ZZZZY)"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10810&pm=8458

Writer:

Petr

Testers:

PabloGilberto , Olexiy , gawry , ivan_metelsky

Problem categories:

Math, Sorting, String Manipulation