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)