你的任务是模拟洪水灾害。
对于给定的横截面图,给出淹没部分的面积。
假设该地区不断地下雨,而从该地区溢出的水正在落入两侧的海中。 例如,对于上面的横截面图,雨水将产生洪水,其面积分别为 4、2、1、19 和 9。
输入
在一行中给出一个字符串,分别用"/",""和"_"表示斜坡和平原。 例如,上面例子的区域由字符串“\ \ / / / \ _ / \ / \ \ \ \ / _ / \ \ / / / _ _ \ \ \ _ \ \ / _ \ / _ / ”给出。
输出
按以下格式输出:
AK
L1,L2,…,Lk
在第一行,打印出洪水区域的总面积A。
在第二行,打印出洪水区域的个数K和每个区域的面积Li(从左到右)。
约束
1 ≤ length of the string ≤ 20000
输入样例
\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\
输出样例
35 5 4 2 1 19 9
#include <iostream>
#include <cmath>
#include<stack>
#include<vector>
using namespace std;
struct node
{
int left;
int area;
};
int main()
{
stack<int> s;
vector<struct node> v;
string str;
cin>>str;
int num=str.length();
int sum=0; //总面积
struct node n;
for(int i=0;i<num;i++)
{
if(str[i]=='\\')
s.push(i);
if(str[i]=='/'&&!s.empty())
{
int j=s.top();
s.pop();
sum+=i-j;
n.left=j;
n.area=i-j;
while(!v.empty()&&v.back().left>j) //更新每个区域的面积
{
n.area+=v.back().area;
v.pop_back();
}
v.push_back(n);
}
}
cout<<sum<<endl;
cout<<v.size()<<' ';
for(int i=0; i<v.size(); i++)
{
cout<<v[i].area<<' ';
}
cout<<endl;
system("pause");
return 0;
}