AOJ0522 JOI and IOI

ちょっと解き方が馬鹿な気がする

#include<iostream>
#include<string>
using namespace std;

int j,i;
string s;
int main(){
	while(cin >> s){
		j = i = 0;
	for(int k = 0; k < s.length() - 2; k++){
		if(s[k] == 'J' && s[k+1] == 'O' && s[k+2] == 'I') j++;
		if(s[k] == 'I' && s[k+1] == 'O' && s[k+2] == 'I') i++;
	}
	printf("%d\n%d\n", j,i);
	}
	return 0;
}