Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

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$ $L_1$ $L_2$ ... $L_k$

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

In the second line, print the number of floods $k$ and areas $L_i (i = 1, 2, ..., k)$ for each flood from the left side of the cross-section diagram. Print a space character before $L_i$.

Constraints

$1 \leq$ length of the string $\leq 20,000$

Sample Input 1

\\//

Sample Output 1

4
1 4

Sample Input 2

\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\

Sample Output 2

35
5 4 2 1 19 9

Solution in Scala (judge verdict: Accepted)

import scala.io.StdIn.readLine
class Stack[T](val stack: List[T]) {
    def push(e: T): Stack[T] = new Stack[T](e :: stack)
    def pop(): Stack[T] = stack match {
        case Nil => new Stack[T](Nil)
        case x :: xs => new Stack[T](xs)
    }
    def top(): Option[T] = stack match {
        case Nil => None
        case x :: xs => Option(x)
    }
    def isEmpty(): Boolean = stack == Nil
    def toList(): List[T] = stack
}
object Stack {
    def apply[T]() = new Stack[T](Nil)
}
object Main {
    def main(args: Array[String]): Unit = {
        val input = readLine.split("").toList
        val (areas, sum) = calcArea(input)
        if (sum == 0) {
            println("0")
            println("0")
        } else {
            println(sum)
            println(areas.length + " " + areas.mkString(" "))
        }
        
    }
    def calcArea(input: List[String]): (List[Int], Int) = {
        def go(list: List[(String, Int)], stack1: Stack[Int], stack2: Stack[(Int, Int)], sum: Int): (List[Int], Int) = list match {
            case Nil => (stack2.toList.map(_._2).reverse, sum)
            case (x @ (symbol, index)) :: xs => symbol match {
                case "_" => go(xs, stack1, stack2, sum)
                case "\\" => go(xs, stack1.push(index), stack2, sum)
                case "/" => stack1 match {
                    case s1 if s1.isEmpty => go(xs, stack1, stack2, sum)
                    case _ => {
                        val top = stack1.top.get
                        val area = index - top
                        go(xs, stack1.pop, sumStack(stack2, top, area), sum + area)
                    }
                }
            }
        }
        def sumStack(stack2: Stack[(Int, Int)], index: Int, area: Int): Stack[(Int, Int)] = stack2 match {
            case s if stack2.isEmpty => stack2.push((index, area))
            case s => s.top.get match {
                case (i, a) if i > index => sumStack(s.pop, index, area + a)
                case _ => s.push((index, area))
            }
        }
        go(input.zipWithIndex, Stack[Int](), Stack[(Int, Int)], 0)
    }
}