ZOJ problem Set-3829known Notation (greedy)

Source: Internet
Author: User

ZOJ problem Set-3829known Notation (greedy)

Topic links

main topic: give you a suffix expression (only numbers and symbols action:
1, wherever this expression is inserted ' ' or number (one digit).
2, swap the characters of this expression in whatever two positions.

Problem Solving Ideas:
The first thought of a good complex, the result is still missing a certain situation, has been difficult to get through, is stuck in the "", the number is not enough to insert or replace good.
In fact, just think: first, the number of numbers at least if the number of symbols + 1. First, the number of numbers and symbols. The numbers are not enough to insert the natural better, otherwise the replacement is better, and the insertion number is as good as the front, replacing the "replacement"in the back better.
Note: All numbers are in the case.

Code:

#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intMAXN =1005;CharSTR[MAXN];intMain () {intTscanf("%d", &t); while(t--) {scanf('%s ', str);intNum, op, ans;intLen =strlen(str); ans = num = op =0; for(inti =0; i < Len; i++)if(Str[i] = =' * ') op++; num = len-op;if(!OP) {printf("0\n");Continue; }if(Str[len-1] !=' * ') {ans++; for(inti =0; i < Len; i++)if(Str[i] = =' * ') {swap (str[i], Str[len-1]); Break; }        }intCNT =0; for(inti =0; i < Len; i++) {if(Str[i] = =' * ') {if(CNT >1) cnt--;Else{if(Num >= op +1) { for(intj = Len-1; J >=0; j--)if(Str[j]! =' * ') {swap (str[j], str[i]);                                cnt++; ans++; Break; }                    }Else{ans++; num++;if(!cnt)                            {i--; CNT =1; }                    }                }            }Elsecnt++; }printf("%d\n", ans); }return 0;}

ZOJ problem Set-3829known 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.