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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

	public static void main(String[] args) throws NumberFormatException,
			IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		char[] input = br.readLine().toCharArray();
		int[] level = new int[input.length + 1];
		Map<Integer, Integer> levelMap = new HashMap<Integer, Integer>();
		List<Integer> startIndex = new ArrayList<Integer>();
		List<Integer> endIndex = new ArrayList<Integer>();

		level[0] = 100;
		levelMap.put(100, 1);
		for (int i = 1; i < input.length + 1; i++) {
			char c = input[i - 1];

			switch (c) {
				case '_' :
					level[i] = level[i - 1];
					break;
				case '/' :
					level[i] = level[i - 1] + 1;
					break;
				case '\\' :
					level[i] = level[i - 1] - 1;
					break;
			}

			if (levelMap.containsKey(level[i])) {
				levelMap.put(level[i], levelMap.get(level[i]) + 1);
			} else {
				levelMap.put(level[i], 1);
			}
		}

		// for (Map.Entry<Integer, Integer> entry : levelMap.entrySet()) {
		//	System.out.println(entry.getKey() + ", " + entry.getValue());
		// }

		MAINLOOP : for (int i = 0; i < level.length - 1;) {

			while (input[i] != '\\') {
				levelMap.put(level[i], levelMap.get(level[i]) - 1);
				if (i + 1 < input.length)
					i++;
				else
					break MAINLOOP;
			}

			if (levelMap.get(level[i]) > 1) {
				levelMap.put(level[i], levelMap.get(level[i]) - 1);

				int j;
				for (j = i + 1; j < level.length; j++) {
					if (level[i] == level[j]) {
						break;
					}
					levelMap.put(level[j], levelMap.get(level[j]) - 1);
				}

				startIndex.add(i);
				endIndex.add(j);
				i = j;
			} else {
				boolean existSameLevel = false;
				int targetLevel = 0;
				for (int j = level[i] - 1; levelMap.containsKey(j); j--) {
					if (levelMap.get(j) > 1) {
						existSameLevel = true;
						targetLevel = j;
						break;
					}
				}

				if (existSameLevel) {
					levelMap.put(level[i], levelMap.get(level[i]) - 1);
					int j;
					for (j = i + 1; j < level.length; j++) {
						if (level[j] == targetLevel) {
							break;
						}
						levelMap.put(level[j], levelMap.get(level[j]) - 1);
					}
					startIndex.add(j);

					for (j += 1; j < level.length; j++) {
						levelMap.put(level[j], levelMap.get(level[j]) - 1);
						if (level[j] == targetLevel) {
							break;
						}
					}

					endIndex.add(j);
					i = j;
				} else {
					break;
				}
			}
		}

		int[] water = new int[startIndex.size()];
		int waterNum = 0;
		int allSum = 0;
		
		
		for (int i = 0; i < startIndex.size(); i++) {
			int start = startIndex.get(i);
			int end = endIndex.get(i);
			// System.out.println(startIndex.get(i) + " -> " + endIndex.get(i));

			int sum = 0;
			
			for (int j = start; j < end; j++) {
				boolean isUp = false;
				boolean isExist = false;
				
				if (input[j] != '\\')
					continue;
				
				int length = 0;
				
				for (int k = j+1; k <= end; k++) {
					length++;
					if (k < input.length && input[k] == '/')
						isUp = true;
					
					if (level[j] == level[k]) {
						isExist = true;
						break;
					}
				}

				if (isUp && isExist) {
					// System.out.println("length = " + length);
					sum += (length * 2 - 2) / 2;
				}
			}
			
			if (sum > 0) {
				water[waterNum++] = sum;
				allSum += sum;
			}
		}

		System.out.println(allSum);
		System.out.print(waterNum);
		
		for (int i = 0; i < waterNum; i++) {
			System.out.print(" " + water[i]);
		}
		System.out.println();

		br.close();
	}

}