题干:
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.
OutputOutput a single integer, the maximum number of times "VK" can appear as a substring of the given string after changing at most one character.
ExamplesVK
1
VV
1
V
0
VKKKKKKKKKVVVVVVVVVK
3
KVKV
1
题目大意:给你一个字符串,改其中一个字母或者不改,然后让你从中找有几个VK。
解题报告: 思维题啊,找几组数据测试一下就找出规律来了,就是先去掉VK的(记录下来ans个),然后看有没有VV或者KK,有的话就输出ans+1,没有的话就输出ans
ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 100 + 5;
char a[N];
bool bk[N];
int ans = 0, flag = 0;
int main()
{
cin>>a;
int len = strlen(a);
for(int i = 0; i < len; i++) {
if(a[i] == 'V' && a[i + 1] == 'K') {
ans++;
bk[i]=1;
bk[i+1]=1;
}
}
for(int i = 0; i < len; i++) {
if(bk[i]==0&&bk[i+1]==0&&a[i]==a[i + 1]) flag=1;
}
if(flag==1) ans++;
printf("%d\n", ans);
return 0 ;
}