VariableSolve
TCO05 Sponsor Rd 1 · 2005-08-16 · by AdminBrett
TCO05 Sponsor Rd 1 · 2005-08-16 · by AdminBrett · Math
Problem Statement
Problem Statement
You will be given an equation of the form <side>=<side> where <side> is a list of <term>s separated by <op>s. Each <op> is either + or -, and each <term> is a positive length string of uppercase letters. Each letter denotes a variable taking on real values. No letter will occur in a <term> more than twice.
The <term>s are products of variables. As you would expect, the + and - operators take lower precedence than the multiplications within the <term>s. For example,String[] containing all such variables that satisfy this requirement. If a variable can be set to n distinct values all of which satisfy this requirement, then that variable should occur n times in the return. If a variable can be set to an infinite number of distinct values that satisfy this requirement, it should not be returned. The return should be sorted in alphabetical order.
The <term>s are products of variables. As you would expect, the + and - operators take lower precedence than the multiplications within the <term>s. For example,
ABC+ADF=PQ-PQis a properly formatted equation. Sometimes, by fixing a single variable at a particular value, we can force the entire equation to always be true. Return a
Constraints
- equation will contain between 3 and 50 characters inclusive.
- equation will be formatted as described in the problem statement.
Examples
0)
"ABCD+ABCD-ABCD=ABCD"
Returns: { }
1)
"ABA+BA+B=P-P"
Returns: {"B" }
2)
"ABA+BA+B=P-P+B"
Returns: {"A", "A", "B" }
3)
"ABA+BA+B=P-P+B+B"
Returns: {"A", "A", "B" }
4)
"P=NP"
Returns: {"N", "P" }
Simpler than it looks: let N = 1 or P = 0.
5)
"RED+BLUE=PURPLE"
Returns: {"E" }
Only E = 0 guarantees the equation holds.
6)
"ABCD+ABCD-ABCD=ABCD"
Returns: { }
Each variable has an infinite number of values causing the equation to hold.
7)
"BAA+BA+B=P-P"
Returns: {"B" }
We cannot consider the complex solutions for A.
Submissions are judged against all 104 archived test cases, of which 8 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class VariableSolve with a public method vector<string> getSolutions(string equation) · 104 test cases · 2 s / 256 MB per case