题目描述
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
L
1
L
2
L
3
⋅
⋅
⋅
A\\ KL_{1}L_{2}L_{3}···
AKL1L2L3⋅⋅⋅
In the first line, print the total area
A
A
A of created floods.
In the second line, print the number of floods
k
k
k and areas
L
i
(
i
=
i
,
2
,
⋅
⋅
⋅
,
k
)
L_i(i =i,2,···,k)
Li(i=i,2,⋅⋅⋅,k) for each flood from the left side of the cross-section diagram. Print a space character before
L
i
L_i
Li.
Sample Input 1
\//
Sample Output 1
4
1 4
Sample Input 2
\///_ //\\//\///__\_\//_/\
Sample Output 2
35
5 4 2 1 19 9
题目大意
就是用一串字符串表示一个地形断面图。“ \ ”表示左上右下倾斜,“ / ”表示右上左下倾斜,“ _ ”表示平面。然后我们要计算这个断面图最后能积多少水。我们可以先假设有连续不停的大雨,这样溢出多余的雨水将会从地形的两侧流入左右的大海。
实现思路
这题一开始我不会做,书上讲的是用栈,有点类似于那个括号匹配的思想。因为这个积水面是一个平衡的水面,所以水的左面和右面的斜面应该是一对一对应起来的。所以我们只需要统计有多少个斜面对以及它们之间的距离即可。
首先我们先计算总面积:
我们可以将整个断面图看成一个Y-X的平面直角坐标系,然后开始遍历
s
i
s_i
si:
- 如果是“ \ ”,就将该字符的位置 i i i压入栈 S 1 S1 S1
- 如果是“ / ”,就从S1中取出与之相对应的“ \ ”的位置 i t i_t it,算出“ / ”和相对应的“ \ ”的距离 i − i t i-i_t i−it并累加的总面积里
- 如果是“ _ ”,就将“ \ ”和“ / ”的距离加1。
其次是各积水处面积的算法:
我们需要另外一个栈
S
2
S2
S2,这个栈的每个元素包含一对数据,分别是该积水处最左侧“ \ ”的位置和该积水处当前的面积。随着不断读取“ / ”,该积水处的面积会不断增加。接下来,新形成的面积 = 当前
S
2
S2
S2中的两个面积之和 + 新形成的
i
−
j
i - j
i−j部分的面积。我们需要从
S
2
S2
S2中取出被引用的多个面积,再将新算出的面积压入
S
2
S2
S2
实现细节
准备阶段
stack<int> s1;
stack<pair<int, int>> s2; //准备两个栈
char ch; //读取每个字符
int sum = 0; //存储积水总面积
计算阶段
由于不确定要输入多少字符,而且我们需要逐个判断字符并记录每个字符的位数,所以采用了for循环,将 c i n > > c h cin>>ch cin>>ch放入进了判断中,我们在输入完毕后,可以输入Ctrl+Z来结束输入。
for (int i = 0; cin >> ch; i++)
{
if (ch == '\\') //判断"/"需要添加转义字符
s1.push(i); //将该处的位置入栈
else if (ch == '/' && s1.size() > 0)
//如果为"/"且有与之对应的"\",我们需要记录"/"的位置,并计算当前的积水面积i-j
{
int j = s1.top();
s1.pop();
sum += i - j;
int a = i - j;
//这里是合并多个积水处的面积
//如果s2中存的积水处最左侧的位置大于当前"\"对应左侧的位置,则需要合并
while (s2.size() > 0 && s2.top().first > j)
{
a += s2.top().second;
s2.pop();
}
//新的积水面积入栈
s2.push(make_pair(j, a));
}
}
最后用 v e c t o r vector vector来存储不定长的积水处面积即可
vector<int> ans;
while (s2.size() > 0)
{
ans.push_back(s2.top().second);
s2.pop();
}
reverse(ans.begin(), ans.end());
由于栈是先进后出的,所以在栈顶的面积是图中相对靠右部的面积。直接输出积水的面积顺序是相反的。所以我们要 r e v e r s e reverse reverse一下
vector自带翻转元素的函数reverse(begin,end)
begin:起始位置的迭代器
end:终止位置的迭代器
最后附上完整代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<int> s1;
stack<pair<int, int>> s2;
char ch;
int sum = 0;
for (int i = 0; cin >> ch; i++)
{
if (ch == '\\')
s1.push(i);
else if (ch == '/' && s1.size() > 0)
{
int j = s1.top();
s1.pop();
sum += i - j;
int a = i - j;
while (s2.size() > 0 && s2.top().first > j)
{
a += s2.top().second;
s2.pop();
}
s2.push(make_pair(j, a));
}
}
vector<int> ans;
while (s2.size() > 0)
{
ans.push_back(s2.top().second);
s2.pop();
}
reverse(ans.begin(), ans.end());
cout << sum << endl;
cout << ans.size();
int len = ans.size();
for (int i = 0; i < len; i++)
{
cout << " ";
cout << ans[i];
}
cout << endl;
return 0;
}
这里是题目链接ALDS1_3_D