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 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
#include
#include
#include
using namespace std;
const int maxn=2e4+10;
// 本来不想用结构体,对于这种两个绑定的,可以用pair,可是用pair之后发现程序运行的很不成功,以为是pair用错了语法,
// 毕竟c++我暂时是自学,pair也是只要从网上看的,于是就把pair改为了结构体,结果运行仍是很不成功,改了很久才发现真正的问题,
struct node //最后才知道pair没有用错,不过都写成结构体了,也就不再改为pair了
{
int first,second;
};
stacks; //存储面积信息,位于字符串的第i位开始的'\',以其为最左边的拦截面积,存储的是,(i,si)
stackq; //存储第几位是'\\'
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.firstif(s1.first若发现存在正确的条件之一时会直接显示正确,并且之后的条件不再进行检查,而&&则是如果遇见错误的条件之一时,直接判断为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 su[i]=s.top().second,s.pop();
for(int i=k-1;i>=0;i--)
printf(" %d",su[i]);
cout< return 0;
}



你可能感兴趣的:(ALDS1_1_3_D Areas on the Cross-Section Diagram 遇见了几个有意思的语法问题)