"Greedy + little Idea" Zoj-3829 known Notation

Source: Internet
Author: User

Borrowed a word from others, thought it was a noble dp ...

A drill a look is a Polish-style problem, a little confused also thought to use the suffix expression or DP below what what, the second half of the competition began to study this problem to find greedy can engage, but read the wrong question!! Exchange can be any two-character exchange, however, it is considered that only adjacent numeric characters with the ' * ' character exchange .../(ㄒoㄒ)/~~ ... But after the game to find the details of the problem when the consideration is not good, or should exercise their own logical collation ...

How greedy ...

The first step, the whole digital string is legal, direct output 0 can be;

The second step, the number is not enough to add. The so-called number is enough, that is, the number of numbers is at least 1 more than the number of asterisks, otherwise the missing number (AL+1-DI) can be added at the beginning, this is the most direct ideal. Correspondingly, this is also equivalent to the number of additional operations carried out (AL+1-DI) times;

The third step, under the condition that the number of numbers and the number of asterisks are valid, the position of the illegal asterisk should be exchanged, and the exchange strategy is a digital interchange with the asterisk, so that it is possible to ensure that each asterisk is legal before it may be misunderstood by someone:

1  for(inti =0; i < Len; i++)2 {3     if(Str[i] = ='*') ta++;4     Elsetd++;5     if(td-ta<1)6     {7td++; ta--; ans++;8     }9}

Because all numbers and the number of characters are already legal, so large can not consider the specific location of the exchange, to ensure that the asterisk before the legal exchange will always be legal.

Attached code:

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <string>6 using namespacestd;7 intMain ()8 {9     intT CIN >>T;Ten      while(t--) One     { A         stringstr; -CIN >>str; -         intLen =str.length (); the  -         intAl, di; Al = di =0;//the number of initial strings and the number of asterisks -          for(inti =0; i < Len; i++)if(Str[i] = ='*') al++; -         if(Al = =0) {printf ("0\n");Continue;} +DI = len-al; -         intNeed;//whether numeric characters are missing +         if(Di-al >=1) Need =0;ElseNeed = al+1-di; A  at         intTD, TA; TD = Need; TA =0; -         intAns =need; -          for(inti =0; i < Len; i++) -         { -             if(Str[i] = ='*') ta++; -             Elsetd++; in             if(td-ta<1) -             { totd++; ta--; ans++; +             } -         } thecout << ans <<Endl; *     } $     return 0;Panax Notoginseng}
View Code

"Greedy + little Idea" Zoj-3829 known Notation

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.