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!