Vicious Keyboard
Tonio has a keyboard with only two letters, "V" and "K".
One day, he has typed out a string s with only these two letters. He really likes it when the string "VK" appears, so he wishes to change at most one letter in the string (or do no changes) to maximize the number of occurrences of that string. Compute the maximum number of times "VK" can appear as a substring (i. e. a letter "K" right after a letter "V") in the resulting string.
The first line will contain a string s consisting only of uppercase English letters "V" and "K" with length not less than 1 and not greater than 100.
Output a single integer, the maximum number of times "VK" can appear as a substring of the given string after changing at most one character.
VK
1
VV
1
V
0
VKKKKKKKKKVVVVVVVVVK
3
KVKV
1
For the first case, we do not change any letters. "VK" appears once, which is the maximum number of times it could appear.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <memory.h>
#define MAX 102
using namespace std;
int main(){
char str[102];
scanf("%s",str);
int ans=0;
char *pstr;
pstr=str;
do{
pstr=strstr(pstr,"VK");
if(pstr){
ans++;
pstr++;
continue;
}else break;
}while(1);
if(strstr(str,"VVV")){
ans++;
}else if(strstr(str,"KK")==str){
ans++;
}else if(strstr(str,"KKK")){
ans++;
}else {
reverse(str,str+strlen(str));
if(strstr(str,"VV")==str)ans++;
}
printf("%d\n",ans);
return 0;
}