Codeforces 801 A.Vicious Keyboard & Jxnu Group Programming Ladder Tournament 2017江西师大新生赛 L1-2.叶神的字符串
2 seconds
256 megabytes
standard input
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.
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.
这个题和师大的新生赛的一个题好像好像。。。
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
Input
每组测试数据输入一串由'Y','S'组成的字符串。(字符串长度最多为10000)
Output
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,所以没超时。。。
Codeforces 801 A.Vicious Keyboard & Jxnu Group Programming Ladder Tournament 2017江西师大新生赛 L1-2.叶神的字符串的更多相关文章
- 【codeforces 801A】Vicious Keyboard
[题目链接]:http://codeforces.com/contest/801/problem/A [题意] 一个字符串只由VK组成; 让你修改一个字符; 使得剩下的字符串里面子串VK的个数最大; ...
- AC日记——Vicious Keyboard codeforces 801a
801A - Vicious Keyboard 思路: 水题: 来,上代码: #include <cstdio> #include <cstring> #include < ...
- 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 ...
- Codeforces Gym101606 A.Alien Sunset (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))
2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017) 寒假第一次组队训练赛,和学长一起训练,题目难度是3颗星,我和猪队友写 ...
- Codeforces Gym101572 B.Best Relay Team (2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017))
2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017) 今日份的训练,题目难度4颗星,心态被打崩了,会的算法太少了,知 ...
- codeforces#1248D2. The World Is Just a Programming Task(括号匹配转化为折线处理)
题目链接: http://codeforces.com/contest/1248/problem/D2 题意: 可以执行一次字符交换的操作 使得操作后的字符串,循环移位并且成功匹配的方案最多 输出最多 ...
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)(A.思维题,B.思维题)
A. Vicious Keyboard time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) 题解【ABCDE】
A. Vicious Keyboard 题意:给你一个字符串,里面只会包含VK,这两种字符,然后你可以改变一个字符,你要求VK这个字串出现的次数最多. 题解:数据范围很小,暴力枚举改变哪个字符,然后c ...
- 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 ...
随机推荐
- 【bzoj5070】危险的迷宫 费用流
题目描述 JudgeOnline/upload/201710/55.doc 输入 第一行是两个整数A与B(1≤A,B≤10),中间用空格分隔,表示该迷宫是A行B列的. 第2行至第A+1行,每行有B个1 ...
- 【bzoj2330】[SCOI2011]糖果 差分约束系统
题目描述 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配 ...
- DataBase -- Employees Earning More Than Their Managers My Submissions Question
Question: The Employee table holds all employees including their managers. Every employee has an Id, ...
- [Leetcode] count and say 计数和说
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 安卓下拉刷新空间SwipeRefreshLayout的基本使用
1.先写布局文件 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh" androi ...
- 480000 millis timeout while waiting for channel to be ready for write异常处理
2014-08-25 15:35:05,691 ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: DatanodeRegistration( ...
- JS alert()、confirm()、prompt()的区别
这三个都是属于弹框类型的 使用警告.提示和确认消息框来获得用户的输入.这些消息框是 window 对象的接口方法.由于 window 对象位于对象层次的顶层,因此实际应用中不必使用这些消息框的全名(例 ...
- jsp中的路径问题
在学jsp的时候我每次都遇到路径的问题,每次都不能够说100%的把这搞定,这让我很烦恼,今天下午花了点时间来把路径问题整理了下. 一:首先我们在加载项目(我的项目名称是FinalExam)是的路径是h ...
- MyBatis查询结果resultType返回值类型详细介绍
一.返回一般数据类型 比如要根据 id 属性获得数据库中的某个字段值. mapper 接口: // 根据 id 获得数据库中的 username 字段的值 String getEmpNameById( ...
- 【HDU5772】String Problem [网络流]
String Problem Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description Input Ou ...