500.Quacking string matches
My Solution
declare a string array;
//"quack"
when first meet 'q',add to the string which index is supposed to be as small sa possible, unless add to a empty string from the array;
then meet 'u' we shall to find a string which is ended by q, 'a','c','k' are nearly the same as 'u';
else return -1;
paste my code ☺
// BEGIN CUT HERE
// END CUT HERE
#line 5 "Quacking.cpp"
#include
#include
using namespace std;
class Quacking {
public:
string str[501]; // 2500/5 => 500
int quack(string s) { //"quack"
int sz = s.size(), smallsz, crr = -1;
for(int i = 0; i < sz; i++){
crr = -1;
if(s[i] == 'q'){
for(int j = 0; j < 500; j++){
smallsz = str[j].size();
if(smallsz == 0) {str[j] = 'q'; crr = 1; break;}
else if(str[j][smallsz-1] == 'k') {str[j] += 'q'; crr = 1; break;}
}
if(crr == -1) return -1;
}
else if(s[i] == 'u'){
for(int j = 0; j < 500; j++){
smallsz = str[j].size();
if(smallsz == 0) {return -1;}
else if(str[j][smallsz-1] == 'q') {str[j] += 'u'; crr = 1; break;}
}
if(crr == -1) return -1;
}
else if(s[i] == 'a'){
for(int j = 0; j < 500; j++){
smallsz = str[j].size();
if(smallsz == 0) {return -1;}
else if(str[j][smallsz-1] == 'u') {str[j] += 'a'; crr = 1; break;}
}
if(crr == -1) return -1;
}
else if(s[i] == 'c'){
for(int j = 0; j < 500; j++){
smallsz = str[j].size();
if(smallsz == 0) {return -1;}
else if(str[j][smallsz-1] == 'a') {str[j] += 'c'; crr = 1; break;}
}
if(crr == -1) return -1;
}
else if(s[i] == 'k'){
for(int j = 0; j < 500; j++){
smallsz = str[j].size();
if(smallsz == 0) {return -1;}
else if(str[j][smallsz-1] == 'c') {str[j] += 'k'; crr = 1; break;}
}
if(crr == -1) return -1;
}
}
int ans = 0;
for(int i = 0; i < 500; i++){
if(str[i].size()) ans++;
}
return ans;
}
};
非特殊说明,本博所有文章均为博主原创,未经许可不得转载。
https://www.prolightsfxjh.com/
Thank you!
------from ProLightsfx
非特殊说明,本博所有文章均为博主原创,未经许可不得转载。
如经许可后转载,请注明出处:https://prolightsfxjh.com/article/topcoder-srm-687-div-2-500-quacking/
共有 0 条评论