ALDS1_1_3_D Areas on the Cross-Section Diagram 遇见了几个有意思的语法问题
Your task is to simulate a flood damage.
For a given cross-section diagram, reports areas of flooded sections.
Assume that rain is falling endlessly in the region and the water overflowing from the region is falling in the sea at the both sides. For example, for the above cross-section diagram, the rain will create floods which have areas of 4, 2, 1, 19 and 9 respectively.
Input
A string, which represents slopes and flatlands by '/', '\' and '_' respectively, is given in a line. For example, the region of the above example is given by a string "\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\".
output
Report the areas of floods in the following format:
A
k L1 L2 ... Lk
In the first line, print the total area A of created floods.
In the second line, print the number of floods kk and areas Li(i=1,2,...,k) for each flood from the left side of the cross-section diagram. Print a space character before Li.
Constraints
- 1≤ length of the string ≤20,000
Sample Input 1
\\//
Sample Output 1
4
1 4
Sample Input 2
\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\
Sample Output 2
35
5 4 2 1 19 9 附上AC代码,至于在解这道题的过程中遇到的许多问题,在代码中都写有注释:
#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std;
const int maxn=2e4+10;
// 本来不想用结构体,对于这种两个绑定的,可以用pair,可是用pair之后发现程序运行的很不成功,以为是pair用错了语法,
// 毕竟c++我暂时是自学,pair也是只要从网上看的,于是就把pair改为了结构体,结果运行仍是很不成功,改了很久才发现真正的问题,
struct node //最后才知道pair没有用错,不过都写成结构体了,也就不再改为pair了
{
int first,second;
};
stack<struct node >s; //存储面积信息,位于字符串的第i位开始的'\',以其为最左边的拦截面积,存储的是,(i,si)
stack<int>q; //存储第几位是'\\'
char str[maxn]; //存储输入的三种字符
int main()
{
int i,j,sum=0;
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='\\') q.push(i);
if(str[i]=='/')
{
if(!q.empty())
{
struct node s1;
s1.second=i-q.top();
sum+=s1.second;
s1.first=q.top();
q.pop();
if(!s.empty()) //之前没有判断这一步,包括下面的是否为空的判断也没有,就导致在第一次引用时,栈s肯定是空,但是我用了s.top(),所以会出错
{
while(1)
{ //这里是关键,判断能不能合并成一个拦截面积
if(!s.empty()&&s1.first<s.top().first) s1.second+=s.top().second,s.pop(); /* 在这里遇到了最后一个问题,我原先写的是
if(s1.first<s.top().first&&!s.empty())但是这么写仍旧程序运行不成功,然后检查bug时看到这里想起了以前看到过的if条件的判断,如果是||连接的,则从左到右判断,
若发现存在正确的条件之一时会直接显示正确,并且之后的条件不再进行检查,而&&则是如果遇见错误的条件之一时,直接判断为false,并且不再检查之后的条件,
如果按照我原先写的,当栈为空时,我却调用了top(),便会理所当然的出现与之前说的同一个问题,但是如果改为我现在写的AC的代码,先判断是不是空,如果是空,
则直接为false,并且不再进行之后的条件判断,便不会出现当栈为空时,仍掉用栈顶元素的现象 */
else {s.push(s1);break;}
}
}
else s.push(s1);
}
else continue;
}
}
int k=s.size();
printf("%d\n%d",sum,k);
int su[maxn];
for(int i=0;i<k;i++) //本来想用队列这种结构了,队列在这里的输出非常简单,可是不能调用队尾元素了(可能可以调用,但是我没遇见过),之前的代码就不能运行,所以就有改为了栈
su[i]=s.top().second,s.pop();
for(int i=k-1;i>=0;i--)
printf(" %d",su[i]);
cout<<endl; //本来没有这句话竟然显示PE,可是一直不明白为什么会有这句话,在题面中也没有写啊,最后还是凭借感觉写上了这句话,然后就AC了
return 0;
}
ALDS1_1_3_D Areas on the Cross-Section Diagram 遇见了几个有意思的语法问题的更多相关文章
- TS各个表 与 SECTION 的解析 CAS原理
TS流,通过一个个的TS包来传送: TS包可以是传送PSI SI等各表的数据包,也可以是传送节目音视频数据(携带的PES包:音视频基本流包)的包:TS携带 PSI SI等表的数据时,各个表以各表对应的 ...
- Transistor 晶体管 场效应 双极型 达林顿 CMOS PMOS BJT FET
Transistor Tutorial Summary Transistor Tutorial Summary Bipolar Junction Transistor Tutorial We can ...
- Circles and Pi
Circles and Pi Introduction id: intro-1 For as long as human beings exist, we have looked to the sky ...
- Video processing systems and methods
BACKGROUND The present invention relates to video processing systems. Advances in imaging technology ...
- allegro使用汇总 [转贴]
1.如何在allegro中取消花焊盘(十字焊盘) set up->design parameter ->shape->edit global dynamic shape parame ...
- Computer Generated Angular Fisheye Projections [转]
Computer GeneratedAngular Fisheye Projections Written by Paul Bourke May 2001 There are two main ide ...
- 每日英语:Risk-Averse Culture Infects U.S. Workers, Entrepreneurs
Americans have long taken pride on their willingness to bet it all on a dream. But that risk-taking ...
- linux heads分析(转)
内核默认的运行地址为PHY_OFFSET+0x8000,即物理地址开始后的0x8000字节处,前面是留给参数用的.参数以atag方式存储,默认放在0x100偏移位置. http://blog.chin ...
- BC in fluent
Boundary conditions in Fluent Table of Contents 1. Boundary Conditions (BC) 1.1. Turbulence Paramete ...
随机推荐
- BZOJ 1878(离散化+线段树)
题面 传送门 分析 首先我们观察到区间范围较大,而区间个数较少,考虑离散化,将所有询问按照右端点进行排序 离散化之后研究区间颜色个数变化的规律 当我们处理到第a[i]个段时,设a[i]上一次出现的地方 ...
- 欧拉函数&欧拉定理&降幂 总结
欧拉函数&欧拉定理&降幂 总结 标签:数学方法--数论 阅读体验:https://zybuluo.com/Junlier/note/1300214 这年头不总结一下是真的容易忘,老了老 ...
- JS同步执行代码
new Promise(function(){initAppToken()}).then(()=> getApps(this.pageInfo).then ...
- C# ASP.NET发送电子邮件System.Net.Mail
1.补充知识 (1)POP3和SMTP服务器是什么? 简单点来说:POP3 用于接收电子邮件 ,SMTP 用于发送电子邮件. (1)POP3具体指什么? POP3(Post Office Protoc ...
- maven整合S2SH
1.pom.xml <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.ap ...
- Solr的学习使用之(五)添加索引数据
1.创建SolrServer类 SolrServer类:提供与Solr实例的连接与通信. 往Solr里添加索引数据,据说有好几种办法,这边利用SolrJ操作solr API完成index操作,具体So ...
- nodejs和npm之间的关系
Node.js是JavaScript的一种运行环境,是对Google V8引擎进行的封装.是一个服务器端的javascript的解释器. 包含关系,nodejs中含有npm,比如说你安装好nodejs ...
- dubbo、web应用 项目结构以及发布目录结构
一.dubbo服务项目结构及发布结构 dubbo 服务项目结构 xxxxx-api 接口类和一些DTO 用于供其他项目依赖 需要提供dubbo服务的接口命名 以Facade结尾 (xxxxxFacad ...
- ubuntu + JetSonNano+OpenCV3.4.8
首先强调一点,如果要配置darknet环境,不建议安装该版本!!! 安装opencv前,建议先检测自己的系统是否已经装过其他版本, 检查方式: (1)查看是否安装opencv库: pkg-config ...
- linux运维、架构之路-SaltStack快速入门
一.SaltStack介绍 SaltStack是一个服务器基础架构集中化管理平台,SaltStack基于Python语言实现,也是基于C/S架构,结合轻量级消息队列(ZeroMQ)与Py ...