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.
InputThe 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.
For the second case, we can change the second character from a "V" to a "K". This will give us the string "VK". This has one occurrence of the string "VK" as a substring.
For the fourth case, we can change the fourth character from a "K" to a "V". This will give us the string "VKKVKKKKKKVVVVVVVVVK". This has three occurrences of the string "VK" as a substring. We can check no other moves can give us strictly more occurrences.
题意:给出一个只含VK的字符串,你可以把其中一个换成另一个,问最多有几个VK。
思路:先跑一边记录有几个VK并标记,再搜一遍有没有相邻的没被标记过的可以构成VK的。
#include<stdio.h>
#include<string.h>
#define N 110
int main()
{
char a[N];
int c=0,m,i,d=0;
gets(a);
m=strlen(a);
for(i=0;i<m;i++)
{
if(a[i]=='V')
{
if(a[i+1]=='K')
{
c++;
a[i]='a';
a[i+1]='a';
i=i+1;
}
}
}
a[-1]=0,a[m]=0;
for(i=0;i<m;i++)
{
if(a[i]=='V')
{
if(a[i+1]=='V')
{
c=c+1;
d=1;
break;
}
}
}
if(d==0)
{
for(i=0;i<m;i++)
{
if(a[i]=='K')
{
if(a[i+1]=='K')
{
c=c+1;
break;
}
}
}
}
printf("%d\n",c);
return 0;
}