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

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef struct {
    int *stk;
    int ptr;
    int size;
} Stk;

bool initStk(Stk *s, int size) {
    if((s->stk = malloc(size * sizeof(int))) == NULL) {
        return false;
    }

    s->ptr = 0;
    s->size = size;
    return true;
}

bool isStkEmpty(Stk *s) {
    return s->ptr <= 0;
}

bool isStkFull(Stk *s) {
    return s->ptr >= s->size;
}

bool pushStk(Stk *s, int x) {
    if(isStkFull(s)) {
        return false;
    }

    s->stk[s->ptr++] = x;
    return true;
}

bool popStk(Stk *s, int *x) {
    if(isStkEmpty(s)) {
        return false;
    }

    *x = s->stk[--s->ptr];
    return true;
}

void teminateStk(Stk *s) {
    free(s->stk);

    s->ptr = s->size = 0;
}

int main(void) {
    Stk stk;

    initStk(&stk, 20000);

    char buf[20000 + 1];
    fgets(buf, sizeof(buf), stdin);
    buf[strlen(buf) - 1] = '\0';

    int bPos;
    for(int i = 0; i < strlen(buf); i++) {
        switch(buf[i]) {
            case '\\':
                pushStk(&stk, i);
                break;
            case '/':
                if(!popStk(&stk, &bPos)) {
                    buf[i] = '*';
                }
            default:;
        }
    }

    for(int i = 0; !isStkEmpty(&stk); i++) {
        popStk(&stk, &bPos);
        buf[bPos] = '*';
    }

    int area[20000] = {0};
    int areaNum = 0;
    int areaSum = 0;
    int pos = 0;
    for(int i = 0; i < strlen(buf); i++) {
        switch(buf[i]) {
            case '\\':
                pushStk(&stk, pos);
                break;
            case '/':
                if(!popStk(&stk, &bPos)) {
                    break;
                }
                area[areaNum] += pos - bPos;
                areaSum += pos - bPos;
                if(isStkEmpty(&stk)) {
                    areaNum++;
                }
                break;
            case '*':
                continue;
            default:;
        }

        pos++;
    }

    printf("%d\n", areaSum);
    printf("%d", areaNum);
    if(0 < areaNum) {
        printf(" ");
    }
    for(int i = 0; i < areaNum;) {
        printf("%d", area[i]);
        i++;
        if(i < areaNum) {
            printf(" ");
        }
    }
    puts("");

    return 0;
}