Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is a lamp in a block, it will light it’s block and the direct adjacent blocks. For example, if there is a lamp at block 3, it will light the blocks 2, 3, and 4.
Given the state of the street, determine the minimum number of lamps to be installed such that each block is lit.
Input
The first line of input contains an integer T (1 ≤ T ≤ 1025) that represents the number of test cases.
The first line of each test case contains one integer N (1 ≤ N ≤ 100) that represents the number of blocks in the street.
The next line contains N characters, each is either a dot ’.’ or an asterisk ’*’.
A dot represents an empty block, while an asterisk represents a block with a lamp installed in it.
Output
For each test case, print a single line with the minimum number of lamps that have to be installed so that all blocks are lit.
|
Sample Input |
Sample Output |
|
3 |
2 |
|
6 |
0 |
|
...... |
1 |
|
3 |
|
|
*.* |
|
|
8 |
|
|
.*.....* |
是的,这是在codeforce上面的一道题,当时写的时候思路和官方给的思路一模一样,可是还是有一些细节没有注意到,比如当字符串是str + 1输入时是可以取到长度len的,for循环中忘记
加上等号了,最近的刷题也不是很顺利,不知道究竟是怎么搞的,可能是写的时候太快的,虽然每道题都写了一遍,可惜都没几个对的,全是都死在了细节上面,以后呀以此为戒,切记!
对了,顺便提醒自己一下,在遇到这种需要改动题目给定条件的时候一定要复制一个改变用的数组来标记,不然可能会自己都不知道错在哪里,希望以后不再犯。
2016-07-26 切记
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
using namespace std; const int MX = 150;
int sign[MX];
int n;
char str[MX]; int main() {
//freopen("input.txt", "r", stdin);
int cas;
while (scanf("%d", &cas) != EOF) {
while (cas--) {
scanf("%d %s", &n, str + 1);
memset(sign, 0, sizeof(sign));
for (int i = 1; i <= n; i++) {
if (str[i] == '*') {
sign[i] = sign[i - 1] = sign[i + 1] = 1;
}
}
int ans = 0;
for (int i = 1; i <= n - 2; i++) {
if (sign[i] == 0 && sign[i + 1] == 0 && sign[i + 2] == 0) {
sign[i] = sign[i + 1] = sign[i + 2] = 1;
ans++;
}
}
for (int i = 1; i <= n - 1; i++) {
if (sign[i] == 0 && sign[i + 1] == 0) {
sign[i] = sign[i + 1] = 1;
ans++;
}
}
for (int i = 1; i <= n; i++) {
if (sign[i] == 0) {
sign[i] = 1;
ans++;
}
}
printf("%d\n", ans);//直接的暴击解决,看到有人喜欢用动态规划,我其实并不想那么多,就用自己的第一感觉做题是最好的
}
}
return 0;
}
Codeforce - Street Lamps的更多相关文章
- ACM: 限时训练题解-Street Lamps-贪心-字符串【超水】
Street Lamps Bahosain is walking in a street of N blocks. Each block is either empty or has one la ...
- 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest
Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...
- 2015 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...
- 【硬件模块】华为NBIOT 使用记录
From: https://liudongdong1.github.io/ 1. background Low power wide area network (LPWAN) has become a ...
- codeforce 985B Switches and Lamps(暴力+思维)
Switches and Lamps time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- codeforce Gym 100425E The Street Escalator(期望,线性递推)
算数学期望,每个人都可以分开来考虑.Xi表示第i个人跑到另外一边的次数. Xi服从二项分布.概率的和是个二项式,(p+1-p)^T,把二项式展开,p的偶次项是留在原来那一边的概率. 可以用((a+b) ...
- 申请邓白氏编码的时候总是提示 Enter a valid Street Address 怎么办?
今天要申请一个苹果开发者公司(Company)账号,然后需要邓白氏编码,然后填写企业的基本信息.其中对于Street Address认真的对照着中文翻译为如下格式: Kang Hesheng buil ...
- 【USACO 2.2】Party Lamps
四种开关,n盏灯,1:改变所有灯状态,2:改变奇数灯状态,3:改变偶数灯状态,4:改变3k+1灯状态 给你按开关的总次数c和部分灯限制条件(开或关),一开始都是开着的.($c \leq 10000,n ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
随机推荐
- 数据结构和算法 – 9.二叉树和二叉查找树
9.1.树的定义 9.2.二叉树 人们把每个节点最多拥有不超过两个子节点的树定义为二叉树.由于限制子节点的数量为 2,人们可以为插入数据.删除数据.以及在二叉树中查找数据编写有效的程序了. 在 ...
- weblogic监控
http://wenku.baidu.com/link?url=tQPQ-dgm7NOkEGj_vemwtsPd6TJ6W3x6_0UBLgw61N982SwPlz-QFxqncsmPGqHwJAEF ...
- ASP.NET MVC中的Global.asax文件
1.global.asax文件概述 global.asax这个文件包含全局应用程序事件的事件处理程序.它响应应用程序级别和会话级别事件的代码. 运行时, Global.asax 将被编译成一个动态生成 ...
- ASP.NET Web API 控制请求频率
参考地址:http://www.cnblogs.com/shanyou/p/3194802.html 安装nuget包:WebApiContrib 很多的api,例如GitHub’s API 都有流量 ...
- Shell编程基础教程7--脚本参数的传递
7.脚本参数的传递 7.1.shift命令 简介: shift n 每次将参数位置向左偏移n位 例子 #!/bin/bash us ...
- 【JAVA集合框架之Map】
一.概述.1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...
- sqlplus 中spool命令的简单用法
spool基本格式: spool 路径+文件名 select col1||','||col2||','||col3||','||col4||'..' from tablename; spool off ...
- 攻城狮在路上(贰) Spring(三)--- Spring 资源访问利器Resource接口
Spring为了更好的满足各种底层资源的访问需求.设计了一个Resource接口,提供了更强的访问底层资源的能力.Spring框架使用Resource装载各种资源,包括配置文件资源.国际化属性文件资源 ...
- 自己制作QQ空间音乐的具体方法
1.打开QQ邮箱找到左栏下方的“文件中转站”--点击收藏文件--上传到收藏 将MP3或WMA音乐文件上传 上传完成点下载 下图: 2.点“保存”将最上面一排的地址全部复制 下图 3.为了更 ...
- waterMarkTextBox
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...