Vicious Keyboard
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.
Input
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
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.
Examples
Input
VK
Output
1
Input
VV
Output
1
Input
V
Output
0
Input
VKKKKKKKKKVVVVVVVVVK
Output
3
Input
KVKV
Output
1
Note
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”数
AC代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
char str[maxn];
bool vis[maxn];
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ios::sync_with_stdio(0),cin.tie(0);
scanf("%s",str);
int len=strlen(str);
int num=0;
bool j=false;
for(int i=0;i<len-1;i++)
{
if(str[i]=='V'&&str[i+1]=='K')
{
num++;
vis[i]=true;
vis[i+1]=true;
}
}
for(int i=0;i<len-1;i++)
{
if((str[i]=='V'&&str[i+1]=='V')||(str[i]=='K'&&str[i+1]=='K'))
{
if(vis[i]==false&&vis[i+1]==false)
j=true;
}
}
if(j)
num++;
printf("%d\n",num);
return 0;
}
下一篇: URL传递中文参数乱码问题
推荐阅读
-
2 Keys Keyboard“编程题”
-
C# monitor keyboard and print pressed key
-
iPad Pro快捷键组合汇总 掌握Smart Keyboard键盘使用方法
-
ReactNative之键盘Keyboard的弹出与消失示例
-
破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988)
-
详解React-Native解决键盘遮挡问题(Keyboard遮挡问题)
-
keyboard dialog 仿微博键盘
-
LeetCode 500. Keyboard Row
-
Permission denied (publickey,password,keyboard-interactive).
-
C# monitor keyboard and mouse actions based on MouseKeyHook.