Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3350
Problem descriptionhave you used # define in C/C ++ code like the code below?
# Include <stdio. h> # define max (A, B) (a)> (B )? (A): (B) int main () {printf ("% d \ n", max (2 + 3, 4); Return 0 ;}
Run the code and get an output: 5, right? You may think it is equal to this Code:
# Include <stdio. h> int max (a, B) {return (a)> (B )? (A): (B);} int main () {printf ("% d \ n", max (2 + 3, 4); Return 0 ;}
But they aren't. though they do produce the same anugh, they work in two different ways. the first code, just replace the max (2 + 3, 4) with (2 + 3)> (4 )? (2 + 3): 4), which calculates (2 + 3) twice. while the second calculates (2 + 3) first, and send the value (5, 4) to function max (a, B), which calculates (2 + 3) only once.
What about Max (max (1 + 2, 2), 3 )? Remember "replace". First Replace: max (1 + 2)> 2? (1 + 2): 2, 3) Second Replace: (1 + 2)> 2? (1 + 2): 2)> 3? (1 + 2)> 2? (1 + 2): 2): 3 ). the Code may calculate the same expression usually times like (1 + 2) abve. so # define isn' t good. in this problem, I'll give you some strings, tell me the result and how many additions (addition) are computed. inputthe first line is an integer T (t <= 40) indicating case number. the next t lines each has a string (no longer than 1000), with max (a, B), digits, '+' only (yes, there're no other characters ). in max (a, B), A and B may be a string with max (c, d), digits, '+ '. see the sample and things will be clearer. outputfor each case, output two integers in a line separated by a single space. integers in output Won 'texceed 1000000. sample input6
Max (1, 0)
1 + max (1, 0)
Max (2 + 1, 3)
Max (4, 2 + 2)
Max (1 + 1, 2) + max (2, 3)
Max (max (1 + 2, 3), max (4 + 5 + 6, max (7 + 8 9) + max (10, max (11, 12), 13 )) sample output1 0
2 1
3 1
4 2
5 2
28 14 train of thought: Use a stack container to simulate the process
# Include <iostream> # Include <Stack> Using Namespace STD; Struct Nod { Int Val, CNT;}; stack <Nod>Ns; stack < Char > SIG; Void S_clear (){ While (! NS. Empty () {ns. Pop ();} While (! Sig. Empty () {Sig. Pop ();}} Int Main (){ Int T; scanf ( " % D " ,& T); getchar (); While (T -- ){ Char STR [ 1010 ]; Gets (STR); s_clear (); nod temp, temp2; temp. CNT = Temp. Val = 0 ; Int I; For (I = 0 ; STR [I]; I ++){ If (STR [I] = ' ( ' ) {Sig. Push (STR [I]); temp. CNT = Temp. Val = 0 ;} Else If (STR [I] = ' , ' ){ While (! Sig. Empty () & Sig. Top () = ' + ' ) {Temp2 = NS. Top (); temp. Val + = Temp2.val; temp. CNT + = Temp2.cnt + 1 ; Sig. Pop (); NS. Pop ();} ns. Push (temp); temp. CNT = Temp. Val = 0 ;} Else If (STR [I] = ' ) ' ){ While (! Sig. Empty () & Sig. Top () = ' + ' ) {Temp2 = NS. Top (); temp. Val + = Temp2.val; temp. CNT + = Temp2.cnt +1 ; Sig. Pop (); NS. Pop ();} temp2 = NS. Top (); If (Temp2.val> Temp. Val) {temp. Val = Temp2.val; temp. CNT + = 2 * Temp2.cnt ;} Else {Temp. CNT = 2 * Temp. CNT + Temp2.cnt;} Sig. Pop (); NS. Pop ();} Else If (STR [I] = ' + ' ) {Sig. Push (STR [I]); NS. Push (temp); temp. CNT = Temp. Val = 0 ;} Else If (STR [I]> = ' 0 ' & STR [I] <= ' 9 ' ) {Temp. Val = 10 * Temp. Val + STR [I]- ' 0 ' ;}} While (! Sig. Empty () & Sig. Top () = ' + ' ) {Temp2 = NS. Top (); temp. Val + = Temp2.val; temp. CNT + = Temp2.cnt + 1 ; Sig. Pop (); NS. Pop ();} printf ( " % D \ n " , Temp. Val, temp. CNT );} Return 0 ;}