A. Vicious Keyboard

来源:互联网 发布:儿童计算机编程 编辑:程序博客网 时间:2024/05/18 10:40
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.


题解:可以想到,扫一遍,然后开一个状态数组,表示是否已经被组合过,然后没有组合的那些字符的状态,V设为1,K设为2,组合好的VK设成3;

然后要找最大的VK数量,只需要找到任意一个没有组合的且状态统一的字符,比如VKVV,状态组里就是3311,这里的11就符合条件,然后把11换成12就行了,VK数量++,直接输出结果即可;

代码如下:

////  main.cpp//  A. Vicious Keyboard////  Created by 徐智豪 on 2017/4/16.//  Copyright © 2017年 徐智豪. All rights reserved.//#include <iostream>#include <string>using namespace std;string s;int dp[110]={0};int state[110]={0};int cur=0;int main(int argc, const char * argv[]) {    cin>>s;    int length=s.size();    if(length==1)    {        cout<<0<<endl;        return 0;    }    for(int i=0;i<length;i++)    {        if(s[i]=='V'&&s[i+1]=='K')        {            state[i]=state[i+1]=3;            i++;            cur++;        }       else if(s[i]=='V')           state[i]=1;       else  if(s[i]=='K')            state[i]=2;    }    int flag=0;    for(int i=0;i<length;i++)    {        if((state[i]==state[i+1])&&state[i]!=3)            flag=1;    }    cout<<cur+flag<<endl;    return 0;}

0 0
原创粉丝点击