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

object Main extends App {
  
  var index = -1
  val inp = StdIn.readLine().trim
  val result = inp.foldLeft(List.empty[Int], List.empty[(Int, Int)], 0){  (acc, e) =>
    var tempAll  = acc._1
    var tempEach = acc._2
    var allmenseki = acc._3
    //println(tempAll)
    //println(tempEach)

    index += 1
    if(e == '\\') {
      tempAll = index :: tempAll  
    } else if(e == '/' && tempAll.length > 0){
      var j = tempAll.head
      allmenseki += index - j
      tempAll = if(tempAll.length == 1) Nil else tempAll.tail
      var a = index - j
      while(tempEach.size > 0 && tempEach.head._1 > j){
        a += tempEach.head._2
        tempEach = tempEach.tail
      }
      tempEach = (j, a) :: tempEach
    }
    (tempAll, tempEach, allmenseki)
  }
  
  println(result._3)
  println(result._2.size + (if(result._2.size == 0) "" else
    " " + result._2.reverse.map(e => e._2).mkString(" ")))
  
}