A. Vicious Keyboard
来源:互联网 发布:儿童计算机编程 编辑:程序博客网 时间:2024/05/18 10:40
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.
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 a single integer, the maximum number of times "VK" can appear as a substring of the given string after changing at most one character.
VK
1
VV
1
V
0
VKKKKKKKKKVVVVVVVVVK
3
KVKV
1
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;}
- A. Vicious Keyboard
- A. Vicious Keyboard codeforce
- A. Vicious Keyboard
- A. Vicious Keyboard
- A. Vicious Keyboard Codeforce
- Codeforces 801A Vicious Keyboard
- CodeForces 801A Vicious Keyboard
- codeforces 801A Vicious Keyboard
- CodeForces 801A Vicious Keyboard 模拟
- Codeforces Round #409 (Div. 2) A. Vicious Keyboard
- CF#801 A. Vicious Keyboard(字符串,水题)
- Codeforces801A Vicious Keyboard
- Codeforces801A Vicious Keyboard
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A. Vicious Keyboard
- 0519 G2n#W2B-B Vicious Keyboard
- CF Vicious Keyboard 构造水题
- CF-Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-A-Vicious Keyboard
- A. Keyboard
- 二分法典型应用(二)最大化最小值 POJ2456 Aggressive cows
- GitHub入门与实践(二)——初始设置Git,以及在GitHub中添加公开密钥
- 【JAVA技术库】分页模糊查询 进阶版
- 容斥原理 && 欧拉函数 && 抽屉原理 总结
- jQuery 选择器整理
- A. Vicious Keyboard
- 运营工具集合
- Bitmap的高效加载和LruCache缓存
- 大型网站典型故障案例1
- 用css画三角形(提示框三角形)
- 十六、定制个性化静态资源
- 微信验证
- 多线程复制文件比单线程慢?
- windows下hyper-V虚拟机使用中的若干问题记录(3)