1 / 3
Apr 2016

I tried this input on my program (don't mind that the characters repeats):

7
C*(A+B)
(A+B+D*E)*C
C*B*A
D+A+F+((((G+C)*R*D)))
V
(A+A)*(B+B)*(C+C)*(D+D)*(E+E)*(F+F)*(G+G)*(H+H)*(I+I)*(J+J)*(K+K)*(L+L)*(M+M)*(N+N)
A*(B+C)*(D+E+F)*(G+H+I+J)*(K+L+M+N+O)*(P+Q+R+S+T+U)*(V+W+X+Y+Z)

It produces:

A*C+B*C
C*D*E+A*C+B*C
A*B*C
C*D*R+D*G*R+A+D+F
V
A*B*C*D*E*F*G*H*I*J*K*L*M*N+A*B*C*D*E*F*G*H*I*J*K*L*M*N+...
A*B*D*G*K*P*V+A*B*D*G*K*P*W+A*B*D*G*K*P*X+...

However, I get WA. I even tried to change the variables to numbers (A=1, B=2, ...) in the two long expressions and calculate the result and the input value matches output value. What am I missing?

  • created

    Apr '16
  • last reply

    Oct '21
  • 2

    replies

  • 788

    views

  • 3

    users

Its very hard to tell whats the problem here, i've got ac by simply going trough expression char by char and calling recursion whenever i hit the bracket to get result from it. So i would go from 0 to n - 1 and whnever i hit '(' i would call recursion to obtain result for that bracket, and when i get it i simply multiply it with my vector of current factors. Also when i get result from bracket i skip my string pointer to end of that bracket.

5 years later

An older problem, but I just solved it.

Try these 4 test cases. They will all provide the same answer. Multiplication has a higher priority than addition.