codechef Little Elephant and Bombs题解
The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy buildings placed in a row and numbered from left to right strating from 0. Each building i (except the first
and the last) has exactly two adjacent buildings with indices i-1 and i+1. The first and the last buildings have just a single adjacent building.
Some of the buildings contain bombs. When bomb explodes in some building it destroys it and all adjacent to it buildings.
You are given the string S of length N, where Si is 1 if the i-th building contains bomb, 0 otherwise. Find for the Little Elephant the number of
buildings that will not be destroyed after all bombs explode. Please note that all bombs explode simultaneously.
Input
The first line contains single integer T - the number of test cases. T test cases follow. The first line of each test case contains the single integer N - the number of buildings. The next line contains the
string S of lengthN consisted only of digits 0 and 1.
Output
In T lines print T inetgers - the answers for the corresponding test cases.
Constraints
1 <= T <= 100
1 <= N <= 1000
Example
Input:
3
3
010
5
10001
7
0000000 Output:
0
1
7
字符串的操作问题。
推断方法非常多种。注意头尾的特殊情况就能够了。
#pragma once
#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std; class LittleElephantandBombs
{
public:
LittleElephantandBombs()
{
int T = 0, N = 0;
scanf("%d", &T);
char s[1001];
while (T--)
{
scanf("%d", &N);
scanf("%s", &s);
char *p = &s[0];
int c = 0;
while (*p)
{
if (*p == '1' && *(p+1) == '0') p += 2;
else if (*p == '0' && *(p+1) != '1') c++, p++;
else p++;
}
printf("%d\n", c);
}
}
}; int littleElephantandBombs()
{
LittleElephantandBombs();
return 0;
}
codechef Little Elephant and Bombs题解的更多相关文章
- codechef Little Elephant and Permutations题解
The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...
- CodeChef November Challenge 2013 部分题解
http://www.codechef.com/NOV13 还在比...我先放一部分题解吧... Uncle Johny 排序一遍 struct node{ int val; int pos; }a[ ...
- CodeChef Little Elephant and Movies [DP 排列]
https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...
- CodeChef Little Elephant and Mouses [DP]
https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中 ...
- codechef February Challenge 2018 简要题解
比赛链接:https://www.codechef.com/FEB18,题面和提交记录是公开的,这里就不再贴了 Chef And His Characters 模拟题 Chef And The Pat ...
- codechef Row and Column Operations 题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- codechef January Lunchtime 2017简要题解
题目地址https://www.codechef.com/LTIME44 Nothing in Common 签到题,随便写个求暴力交集就行了 Sealing up 完全背包算出得到长度≥x的最小花费 ...
- codechef January Challenge 2017 简要题解
https://www.codechef.com/JAN17 Cats and Dogs 签到题 #include<cstdio> int min(int a,int b){return ...
- codechef Sums in a Triangle题解
Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...
随机推荐
- PCB 全景图技术实现
为了对3D模型理解更透,这里采用threejs(WebGL第三方库)实现,刚开始学习入门,为了能看明白基本上每行代码都注释. 如果仅仅是为了实现全景图,可以用photo-sphere-viewer.j ...
- selenium3+python-多窗口、句柄(handle)
一.获取当前窗口句柄 1.元素有属性,浏览器的窗口其实也有属性的,只是你看不到,浏览器窗口的属性用句柄(handle)来识别. 2.人为操作的话,可以通过眼睛看,识别不同的窗口点击切换.但是脚本没长眼 ...
- 初学Hibernate杂乱总结
1.如果在"one"方中(如部门)写有Set属性,但是没有在映射文件中配置,那么,在获取指定部门下的所有员工时,不会报错,但是,Set内的元素个数为0.输出为"[]&qu ...
- 用List表示多重性
练习目标-在类中使用List作为模拟集合操作: 在本练习中,将用List实现银行与客户间的多重关系. 任务 对银行来说,可添加Bank类. Bank 对象跟踪自身与其客户间的关系.用Customer对 ...
- dispatch_sync
dispatch_sync does two things: queue a block blocks the current thread until the block has finished ...
- Cache-Control官方文档
https://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-25#page-21 5.2. Cache-Control The "Cach ...
- js 响应事件
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Robot Framework(六)变量
变量 2.5.1简介 变量是Robot Framework的一个不可或缺的特性,它们可以在测试数据的大多数地方使用.最常见的是,它们用于测试用例表和关键字表中关键字的参数,但所有设置都允许在其值中使用 ...
- lvs直接路由模式DR模式
1)实验所有关闭防火墙systemtcl stop firewalldsystemctl disable firewalldsetenforce 0iptables -F2)配置负载调度器配置虚拟IP ...
- 【转载】IDEA的这八条配置你一定要改!
引言 坦白说,我很少写这种操作类型的文章.因为这种文章没啥新意,大家操作步骤肯定是一样的.然而,我答应了我的同事小阳,给她出一篇!毕竟人家打算从Eclipse转IDEA了,于是以示鼓励,写一篇给她!那 ...