CF - 801A. Vicious Keyboard - 思维

1.题目描述:

A. Vicious Keyboard
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.


2.题意概述:

给你一长串字符串,其中字符只包含V或者K,你可以有一次机会对其中任意某个字符修改一次,也可以不修改,问你最优情况下,整串字符最多包含几个“VK”子串

3.解题思路:

显然,要么把v改成k,要么把k改成v,什么情况可以选择改呢?如果是vvk则中间的v肯定是不能改的,因此很容易发现以下几种情况:

对于v:

1.连续出现三次v(即vvv)一定有种最优策略是把中间v改成k(即vkv)

2.如果结尾连续出现两次v(即vv),一定有种最优策略是把结尾的v改成k(即vk)

对于k:

1.连续出现三次k(即kkk)一定有种最优策略是把中间k改成v(即kvk)

2.如果开头连续出现两次k(即kk),一定有种最优策略是把第一个k改成v(即vk)

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
char ch[N];
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	while (~scanf("%s", ch))
	{
		int len = strlen(ch);
		int cnt = 0;
		for (int i = 0; i < len - 1; i++)
			if (ch[i] == 'V' && ch[i + 1] == 'K')
				cnt++;
		int v = 0, k = 0;
		for (int i = 0; i < len - 1; i++)
			if (ch[i] == 'K' && ch[i + 1] == 'K')
			{
				if (i == 0 || ch[i - 1] == 'K')
				{
					v = 1;
					break;
				}
			}
		for (int i = 1; i < len; i++)
			if (ch[i - 1] == 'V' && ch[i] == 'V')
			{
				if (i == len - 1 || ch[i + 1] == 'V')
				{
					k = 1;
					break;
				}
			}
		printf("%d\n", max(cnt + v, cnt + k));
	}
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}
close

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值