例えばn = 1 の時、 "IOI"でカウントした後 "OI"がきたら絶対2カウント目する。(IOIOIになるから)
n = 2の時も同様に、 "IOIOI"でカウントしたあと"OI"がきたら"IOIOIOI"が出来上がる。
それを利用してうまくやる。
#include <iostream> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int main(){ int n , m; string t = "IO"; while(cin >> n , n){ cin >> m; int ret = 0 , w = 0; bool f = 0; rep(i,m){ char c; cin >> c; if(t[f] == c)w++ , f^=1; else if(c == 'I') w = 1 , f = 1; else if(c == 'O') w = 0 , f = 0; if(w >= n*2+1 ){ if(w%2)ret++; } } cout << ret << endl; } }