1.题目描述:
给你一长串字符串,其中字符只包含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