Topcoder SRM 327 DIV1 300!

Source: Internet
Author: User

Test instructions: Give a string that contains an uppercase letter or a question mark '? '. When a string is defined as ugly, it is possible to find three consecutive vowels or five non-vowel characters in the string; A string is defined as nice, it is not ugly. Now ask, can you change all '? ' becomes a character, making the string a nice or UGLY, if all can, output "47", if only one, output "UGLY" or "nice".

Solution: For the case can be ugly, very simple, will each '? ' into vowels to see if there are 3 consecutive vowels, and then each '? ' into a non-vowel, and then see if there are 5 consecutive non-vowels. For the case of Nice, with greedy to do it (do not want to complicate, otherwise you may enter the pit), first, you can be small, for a continuous question mark (the number of question marks is greater than 1), you can change the question mark, so that the string is nice. For a single question mark, that is, a question mark, the left and right sides are not question marks, this time need to discuss, greedy under the good. After writing has been looking at other people's code, found that there is a better wording, do not need to pre-processing what, direct O (n) sweep, with two variables sentenced to the line.

Best Code

#include <string>using namespace STD;classniceorugly{ Public:BOOLIsvol (Charc) {returnc = =' A '|| c = =' E '|| c = =' I '|| c = =' O '|| c = =' U '; }stringDescribestrings) {BOOLCan1 =0;intNV, NC; NV = NC =0; for(inti =0; I < s.length () && Can1 = =0; i++) {if(S[i] = ='? ') nv++, nc++;Else if(Isvol (S[i])) nv++, NC =0;ElseNV =0, nc++;if(NV >=3|| NC >=5) Can1 =1; }BOOLCan2 =1; NV = NC =0; for(inti =0; I < s.length () && can2 = =1; i++) {if(S[i] = ='? '){if(NV = =2) NV =0, NC =1;Else if(NC = =4) NV =1, NC =0;ElseNV = NC =0; }Else if(Isvol (S[i])) nv++, NC =0;ElseNV =0, nc++;if(NV >=3|| NC >=5) Can2 =0; }if(Can1 && can2)return " A";Else if(Can1 &&!can2)return "UGLY";Else return "Nice"; }};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Topcoder SRM 327 DIV1 300!

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.