Easy(250)
普通の250点問題。そんなに難しい問題でもなかったけど、問題の意味が正直まったく分からなくて無駄に時間使った。
要約すると、'A','B'に塗り分けられたマスがあって、連続する同じシンボルのマスを消していってどっちが残るのか見たいな感じ。
class RowAndCoins {
public:
string getWinner(string cells) {
string ans = "Alice";
int cnta = 0,cntb = 0;
int l = cells.length();
bool f;
if(cells[0] == 'A'){
f = true;
cnta++;
}else{
f = false;
cntb++;
}
for(int i = 0; i < l; i++){
if(f){
if(cells[i] == 'B'){
cntb++;
f = false;
}
}else{
if(cells[i] == 'A'){
cnta++;
f = true;
}
}
}
if(cnta < cntb){
ans = "Bob";
}
return ans;
}
};
連続するマスを圧縮して最後に数えればいいかな、とか思ってた。'B'から消していく場合'A'が'B'と同数以上なら(ってか圧縮後2個以上多いのはありえないけど)'A'が残るわけで、
そう考えると'B'が多くなるのは両端が'B'のときだけだよね。
ってことで↑のコードは駄作。
string getWinner(string cells) {
string ans = "Alice";
if(cells[0] == 'B' && cells[cells.length() - 1] == 'B')
ans = 'Bob';
return ans;
}
こっちの方が断然スマート。
0 件のコメント:
コメントを投稿