Zoj 3829 known notation (Greedy)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.