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 Go (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	CH_UP   = '/'
	CH_DOWN = '\\'
	CH_FLAT = '_'
)

// Go には Generics ないので仕方なく interface{}
type Stack []interface{}

func (s *Stack) push(v interface{}) {
	*s = append(*s, v)
}

func (s *Stack) top() interface{} {
	return (*s)[len(*s)-1]
}

func (s *Stack) pop() interface{} {
	v := s.top()
	*s = (*s)[:len(*s)-1]
	return v
}

type Pair struct {
	start int
	area  int
}

func main() {
	var s string
	fmt.Scanln(&s)

	starts := Stack(make([]interface{}, 0))
	areas := Stack(make([]interface{}, 0))

	for i, c := range s {
		switch c {
		case CH_DOWN:
			starts.push(i)
		case CH_UP:
			if len(starts) == 0 {
				continue
			}

			start := starts.pop().(int)
			area := i - start

			// 自分の'\'~'/'の範囲に含まれる、自分より下のラインの面積を累計する。
			// これで水溜りごとの面積の合計を出せる。
			for len(areas) > 0 {
				p := areas.top().(Pair)
				if start > p.start {
					break
				}
				area += p.area
				areas.pop()
			}

			areas.push(Pair{start, area})
		}
	}

	total := 0
	for _, p := range areas {
		total += p.(Pair).area
	}

	w := bufio.NewWriter(os.Stdout)
	fmt.Fprintln(w, total)

	fmt.Fprint(w, len(areas))
	for _, p := range areas {
		fmt.Fprintf(w, " %d", p.(Pair).area)
	}
	fmt.Fprintln(w)

	w.Flush()
}