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.
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.
数据不大,直接暴力
Ac code:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define fi first
#define se second
#define eps 1
const int maxn=200000+500;
int main() {
char s[105]; int vis[105];
while(~scanf("%s",s)){
memset(vis,0,sizeof vis);
int cnt=0;
int len=strlen(s);
for(int i=0;i<len-1;i++){
if(s[i]=='V'&&s[i+1]=='K'){
cnt++;
vis[i]=vis[i+1]=1;
i++;
}
}
for(int i=0;i<len-1;i++){
if((s[i]=='V'||s[i+1]=='K')&&vis[i]==0&&vis[i+1]==0){
cnt++;
break;
}
}
printf("%d\n",cnt );
}
return 0;
}
本文介绍了一种算法,用于计算通过更改一个字符最多能获得多少次特定字符串VK作为子串出现的方法。输入为只包含V和K的字符串,输出则是最大出现次数。
1299

被折叠的 条评论
为什么被折叠?



