CF

来源:互联网 发布:java 日志 实现 编辑:程序博客网 时间:2024/05/22 14:33

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_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);long _begin_time = clock();#endifwhile (~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_JUDGElong _end_time = clock();printf("time = %ld ms.", _end_time - _begin_time);#endifreturn 0;}close

0 0
原创粉丝点击