Question Link
Question: I will give you a string, but the space is lost. How many operations do you need to perform to make this string a legal inverse Polish style, for example, 12*3*4 is not a legal inverse polish type, but 12*34 * can be regarded as 1 2*34 * as a correct inverse polish type.
Idea: when the number of numbers is more than the number of operators, it is obvious that the number of operations for the exchange is less. You just need to change the operator to the end. The implied meaning in the question is 12. You can think of 1 and 2 as 12, and pay attention to flexibility when doing the question.
When the number of operators is more than the number, it is obvious that inserting a number is the correct method, just insert it forward. The maximum number of valid inverse Polish operators is-1.
# Include <stdio. h> # include <iostream> # include <string. h> using namespace STD; int main () {int N; scanf ("% d", & N); getchar (); char ch [1100]; while (n --) {scanf ("% s", CH); int Len = strlen (CH); int opcnt = 0, numcnt = 0; For (INT I = 0; I <Len; I ++) {If (CH [I] = '*') {opcnt ++ ;}} if (opcnt = 0) {printf ("0 \ n"); continue;} int CNT = 0; // * Number of numbers on the front side int ans = 0; numcnt = len-opcnt; for (in T I = 0; I <Len; I ++) {If (CH [I] = '*') {If (CNT> 1) CNT --; // as long as there are numbers on the front side, * will always be consumed. The number of else {If (numcnt> opcnt) that cannot be consumed can be viewed as multiple numbers, * Change {for (Int J = len-1; j> = 0; j --) {If (CH [J]! = '*') {Swap (CH [I], CH [J]); CNT ++; ans ++; break ;}} else {ans ++; numcnt ++; // The number is not enough. Insert the number if (CNT = 0) // when the first string is * {I --; CNT = 1 ;}}}} else CNT ++;} printf ("% d \ n", ANS);} return 0 ;}View code
Zoj 3829 known notation (Greedy)