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.

这个题和师大的新生赛的一个题好像好像。。。

Jxnu Group Programming Ladder Tournament 2017

L1-2 叶神的字符串

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 23   Accepted Submission(s) : 10

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

众所周知,ACS协会会长叶神学习特别好,算法能力也强,作为一个弱渣的大豪哥特别崇拜叶神,觉得‘Y’‘S’这两个字符特别厉害,所以大豪哥的一个键盘上就只有Y,S两个键,大豪哥用这个键盘打出了一个字符串s,但是他特别的不满意,所以他想改变字符串s中的一个字符(也可以不改变),使得字符串s中可以截取出最大数量的“YS”

Input

多组输入至文件结尾。
每组测试数据输入一串由'Y','S'组成的字符串。(字符串长度最多为10000)

Output

输出至多一次修改后最多有多少个“YS”

Sample Input

YYYS

Sample Output

2

唉,真是让人无语。首先是这个师大的。

代码1:

#include<stdio.h>
#include<string.h>
int main(){
char a[];
int i,flag[],len,num1,num2;
while(gets(a)){
len=strlen(a);
for(i=;i<len;i++)
flag[i]=;
num1=;
for(i=;i<len;i++){
if(a[i]=='S'&&a[i-]=='Y'){
num1++;
flag[i]=;
flag[i-]=;
}
}
num2=;
for(i=;i<len;i++){
if(flag[i]==&&flag[i-]==){
num2++;
flag[i]=; //其实这里去掉更好
flag[i-]=; //楼上+1。。。
if(a[i]=='Y'&&a[i-]=='S')
num2--;
}
}
if(num2>=)
printf("%d\n",num1+);
else
printf("%d\n",num1);
}
return ;
}

修改了一下:

代码2:

#include<stdio.h>
#include<string.h>
int main(){
char a[];
int i,flag[],len,num1,num2;
while(gets(a)){
len=strlen(a);
for(i=;i<len;i++)flag[i]=;
num1=;
memset(flag,,sizeof(flag));
for(i=;a[i]!='\0';i++){
if(a[i]=='S'&&a[i-]=='Y'){
num1++;
flag[i]=;
flag[i-]=;
}
}
num2=;
for(i=;i<len;i++){
if(flag[i]==&&flag[i-]==){
num2++;
flag[i]=;
flag[i-]=;
if(a[i]=='Y'&&a[i-]=='S')num2--;
else break;
}
}
if(num2>=)
printf("%d\n",num1+);
else
printf("%d\n",num1);
}
return ;
}

其实还有一个代码,但是少了个判断条件,想法是一样的,不贴了。。。

然后!!!在做了cf的这个类似题之后发现了新的问题。。。

等会再说吧,要贴cf的代码了。。。

代码1:

#include<stdio.h>
#include<string.h>
int main(){
char a[];
int i,flag[],len,num1,num2;
while(gets(a)){
len=strlen(a);
for(i=;i<len;i++)flag[i]=;
num1=;
memset(flag,,sizeof(flag));
for(i=;a[i]!='\0';i++){
if(a[i]=='K'&&a[i-]=='V'){
num1++;
flag[i]=;
flag[i-]=;
}
}
num2=;
for(i=;i<len;i++){
if(flag[i]==&&flag[i-]==){
num2++; //这里和那个师大的不一样了。
if(a[i]=='V'&&a[i-]=='K')num2--;
else break;
}
}
if(num2>=)
printf("%d\n",num1+);
else
printf("%d\n",num1);
}
return ;
}

VKKVVVKVKVK这组数据如果改成师大的那个字母就wa了,因为flag[]那里错了,(第二个循环里的标记去掉就可以了) 师大的数据水过了。。。

然后!!!cf的这个可以暴力写。

代码2:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=;
char str[N];
char b[N];
int main(){
while(gets(str)){
int len=strlen(str);int maxx=;
if(len==){cout<<<<endl;continue;}
for(int i=;i<len-;i++)if(str[i]=='V'&&str[i+]=='K')
maxx++;
for(int i=;i<len;i++){
char c=str[i];int ans=;
if(str[i]=='V')str[i]='K';
else str[i]='V';
for(int j=;j<len-;j++){
if(str[j]=='V'&&str[j+]=='K')ans++;
}
maxx=max(maxx,ans);
str[i]=c;
}
cout<<maxx<<endl;
}
}

一开始有点没太懂

以VKVKVKVVVV这个为例:第一次变成 KKVKVKVVVV,第二次 VVVKVKVVVV,还是3。。。VKKKVKVVVV ,VKVVVKVVVV,3

每次看他有几个Vk,就这样,最后取最大的,到后面的时候maxx=4,ans=4 所以还是4,但是用暴力写师大的就会超时。

暴力写时间复杂度是O(n^2),那个flag[]的时间复杂度是O(n),

因为师大的字符串长度是10000,所以暴力过不了,会超时。而cf的这个题是100,所以没超时。。。

CodeForces801-A.Vicious Keyboard-暴力的更多相关文章

  1. Codeforces 801 A.Vicious Keyboard & Jxnu Group Programming Ladder Tournament 2017江西师大新生赛 L1-2.叶神的字符串

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

  2. Codeforces801A Vicious Keyboard 2017-04-19 00:16 241人阅读 评论(0) 收藏

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

  3. AC日记——Vicious Keyboard codeforces 801a

    801A - Vicious Keyboard 思路: 水题: 来,上代码: #include <cstdio> #include <cstring> #include < ...

  4. Vicious Keyboard CodeForces - 801A (暴力+模拟)

    题目链接 题意: 给定一个字符串,最多更改一个字符,问最多可以有多少个“VK”子串? 思路: 由于数据量很小,不妨尝试暴力写.首先算出不更改任何字符的情况下有多个VK字串,然后尝试每一次更改一个位置的 ...

  5. [CodeForce 801A] Vicious Keyboard

    题目链接:http://codeforces.com/problemset/problem/801/A 思路:题目中字符串的长度最长100个字符,所以,可以考虑用暴力,先遍历一遍匹配"VK& ...

  6. 【codeforces 801A】Vicious Keyboard

    [题目链接]:http://codeforces.com/contest/801/problem/A [题意] 一个字符串只由VK组成; 让你修改一个字符; 使得剩下的字符串里面子串VK的个数最大; ...

  7. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A B C D 暴力 水 二分 几何

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

  8. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) 题解【ABCDE】

    A. Vicious Keyboard 题意:给你一个字符串,里面只会包含VK,这两种字符,然后你可以改变一个字符,你要求VK这个字串出现的次数最多. 题解:数据范围很小,暴力枚举改变哪个字符,然后c ...

  9. 4.30-5.1cf补题

    //yy:拒绝转载!!! 悄悄告诉你,做题累了,去打两把斗地主就能恢复了喔~~~ //yy:可是我不会斗地主吖("'▽'") ~~~那就听两遍小苹果嘛~~~ 五一假期除了花时间建模 ...

随机推荐

  1. 【Uva10559】Blocks(区间DP)

    Description 题意:有一排数量为N的方块,每次可以把连续的相同颜色的区间消除,得到分数为区间长度的平方,然后左右两边连在一起,问最大分数为多少. \(1\leq N\leq200\) Sol ...

  2. 如何用jQuery实现五星好评

    jQuery是js的一个库,封装了我们开发过程中常用的一些功能,方便我们来调用,提高了我们的开发效率. Js库是把我们常用的功能放到一个单独的文件中,我们用的时候,直接引用到页面里面来就可以了. 接下 ...

  3. c语言库函数测试

    1.函数名: abort功  能: 异常终止一个进程用  法: void abort(void);程序例: #include <stdio.h> #include <stdlib.h ...

  4. CSS height:100%无效

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/38 浏览器根本就不计算内容的高度,除非内容超出了视窗范围(导致滚 ...

  5. Ryz的鬼题

    蚂蚁(ant)[题目描述]  小 R 种了一棵苹果树,这棵树上有 n 个节点(标号从 0 到 n-1),有 n-1 条树枝连接这  n 个节点,这 n 个节点相互连通.每条树枝的长度为 1.  苹果树 ...

  6. .net 4.5 webform 提取ModelState错误信息

    .net4.5以后,webform也可以使用模型绑定和模型验证. user实体: public class User { [Required] [Display(Name = "用户ID&q ...

  7. linux保持管道中颜色显示

    在linux工作中,不同类型的文件以不同的颜色显示,如文件夹显示蓝色,压缩文件显示橘黄色,可执行文件显示为绿色,链接失效文件高亮显示等等: 有时候根据颜色可以快速鉴别,如我有时为了保持目录的完整性,会 ...

  8. primer漏配问题解决

    在对之前的ITS数据(454数据)做split时,发现有一些reads没有被匹配上,但是barcode能够完全匹配,虽然之后的primer在中间漏了一个碱基,导致后面的碱基全部误匹配,从而导致这条re ...

  9. UWP 手绘视频创作工具技术分享系列 - 文字的解析和绘制

    本篇作为技术分享系列的第二篇,详细讲一下文字的解析和绘制,这部分功能的研究和最终实现由团队共同完成,目前还在寻找更理想的实现方式. 首先看一下文字绘制在手绘视频中的应用场景 文字是手绘视频中很重要的表 ...

  10. Jenkins:基于linux构建ivy项目

    Jenkins:基于linux构建ivy项目 (二) 基于以上<Jenkins:VMware虚拟机Linux系统的详细安装和使用教程(一)>的配置再进行对ivy项目构建: 启动tomcat ...