公式
B - モグラの穴 / Mole Holes 解説
by
B - モグラの穴 / Mole Holes 解説
by
MMNMM
印刷ページ
例えば、左からマスを見て行き、「現在いくつ穴が連続しているか」を管理することでこの問題を解くことができます。
実装例は以下のようになります。
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
string S;
cin >> S;
int ans = 0, now = 0;
for (char c : S) {
if (c == '#') { // 穴なら
++now; // 個数を増やして
} else { // 穴でなければ
now = 0; // 0 にする
}
ans = max(ans, now); // 最大値が答え
}
cout << ans << endl;
return 0;
}
N = int(input())
ans = 0
now = 0
for c in input():
if c == '#': # 穴なら
now += 1 # 個数を増やして
else: # 穴でなければ
now = 0 # 0 にする
ans = max(ans, now) # 最大値が答え
print(ans)
投稿日時:
最終更新:
