ALDS1_3_D: Areas on the Cross-Section Diagram

题解
  1. 这题可以将每一处的积水视为一行一行的积水组合而成,这样每行的积水面积就是其’\‘和’/‘相隔的距离,如果’\‘的位置是i,’/'的位置是j,则该行积水的面积为j-i。
  2. 因此可以设置一个整型元素栈s1,每次遇到’\‘就入栈其的位置,每次遇到’/‘并且栈不空就出栈计算距离。每次计算得到的距离与’\'的位置组合成一对整型数据入栈s2(用来保存各积水的面积)。这里存’\‘位置是为了判断是否是同一处积水,如果栈顶元素的’\‘位置要比入栈元素的’\'位置大就说明是同一处积水,要出栈合并。
  3. 这里在设置栈s2元素的时候没有用结构体,而是用了C++标准库类型pair,其的头文件是< utility >,但如果用了std命名空间也可以直接使用pair。使用pair可以方便只有两个成员的情况。
  4. pair对象声明方式:pair<T1, T2> p1; pair<T1, T2> p1(v1, v2);//带初始化声明,以及make_pair(v1, v2);来直接将v1, v2组合成一个pair对象。访问pair对象中的两个元素可以通过p1.first与p1.second来实现,对应v1与v2。
  5. 这里用了< algorithm >头文件中的reverse(first, last)函数来反转vector对象,其作用是反转在[ first, last )范围内的序列,函数无返回值。(注意左闭右开,正好和vector中的begin()和end()函数对应,begin返回的是向量第一个元素的迭代器,end返回的是向量最后一个元素的后一个位置的迭代器)
题目
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:
Ak L1 L2 ... Lk

In the first line, print the total area A of created floods.

In the second line, print the number of floods k 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
代码块
#include <iostream>
#include <stdio.h>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;

int main(void)
{
    stack<int> s1;
    stack<pair<int, int> > s2;//注意这里两个'>'之间要有一个空格,不然会被误认报错。
    char ch;
    int i = 1;
    int sum = 0;
    while((ch = getchar())!='\n')
    {
        if(ch=='\\')//字符'\'要用'\\'才能表示
            s1.push(i);
        else if(ch=='/' && s1.size()>0)
        {
            int j = s1.top();
            s1.pop();
            pair<int, int> temp1, temp2;
            temp1.first = j;
            temp1.second = i-j;
            sum += i-j;
            while(s2.size()>0 && j<s2.top().first)
            {
                temp1.second += s2.top().second;
                s2.pop();
            }
            s2.push(temp1);
        }
        i++;
    }
    vector<int> ans;
    while(!s2.empty())
    {
        ans.push_back(s2.top().second);
        s2.pop();
    }
    reverse(ans.begin(), ans.end());
    cout<<sum<<endl;
    cout<<ans.size();
    for(i=0; i<ans.size(); i++)
        cout<<' '<<ans[i];
    cout<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值