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



Sample input:
\\//


Sample output:
4
1 4

Submission status: Accepted

Solution code:
s=input()
s1=[]
s2=[]
ans=[]
a=0
a1=0
for i in range(len(s)):
    if s[i]=="\\":
        s1.append(i)
    elif s[i]=="/" and s1:
        i2=s1[-1]
        a1=i-i2
        a+=a1
        while s2 and s2[-1][0]>i2:
            a1+=s2[-1][1]
            s2.pop()
        s2.append([i2,a1])
        s1.pop()


n=len(s2)
for j in range(n):
  ans.append(s2[j][1])
print(a)
print(n,*(ans))

Submission status: Accepted

Solution code:
#面積計算
modelmap = list(input())
S1 = []
S2 = []
A = 0
 
for i, l in enumerate(modelmap):
    if l == "\\":
        S1.append(i)
    elif l == "/" and len(S1) > 0:
        ip = S1.pop()
        A += i - ip
        L = i - ip
        while len(S2) > 0 and S2[-1][0] > ip:
            L += S2.pop()[1]
        S2.append([ip, L])
 
print(A)
text2 = "{}".format(len(S2))
for s in S2:
    text2 += " " + str(s[1])
print(text2)

Submission status: Accepted

Solution code:
pos_latest = []

surface = input()

total = []
total_x = []
y = 0
x = 0
total_tmp = 0
for i in range(len(surface)):
    if surface[i] == "\\":
        pos_latest.append(x)
    if surface[i] == "/":
        if pos_latest:
            pos = pos_latest.pop()
            total.append(x-pos)
            total_x.append([pos, x, x-pos])
    x += 1

if sum(total) > 0:
    sorted_total_x = sorted(total_x)
    t_base = sorted_total_x[0][1]
    tmp = sorted_total_x[0][2]
    res = []
    
    for f, t, v in sorted_total_x[1:]:
        if t <= t_base:
            tmp += v
        else:
            res.append(int(tmp))
            tmp = v
            t_base = t
        if sum(res)>=sum(total):
            break
    res.append(int(tmp))

    print(sum(res))
    print(len(res), *res, sep=" ")
    
else:
    print(sum(total))
    print(0)

Submission status: Accepted

Solution code:
data =input()
s1 =[]
ans =[]
memo=-1
for i in range(len(data)):
	if data[i]=='\\':
		s1.append(i)
	if data[i]=='/':
		if len(s1)==0:pass
		# elif len(s1)==1:
		# 	ans.append(i-s1.pop())
		else:
			a = i-s1[-1]
			while len(ans)!=0:
				if ans[-1][1]<=s1[-1]:break
				a+=ans.pop()[0]
			ans.append([a,s1.pop()])

	# print(ans)
ans2=[int(ans[i][0]) for i in range(len(ans))]
print(sum(ans2))
print(len(ans),end='')
if len(ans):
	print('',end=' ')
print(' '.join(list(map(str,ans2))))

Submission status: Accepted

Solution code:
S = raw_input()

areas = []
stack = []
for i,c in enumerate(S):
    if c == "\\":
        stack.append(i)
    elif c == "/":
        if len(stack) > 0:
            j = stack.pop()
            area = i-j
            while len(areas) > 0:
                p,a = areas.pop()
                if j < p:
                    area += a
                else:
                    areas.append((p, a))
                    break
            areas.append((j, area))

A = map(lambda x:x[1], areas)
print sum(A)
if len(A) == 0:
    print "0"
else:
    print len(A), " ".join(map(str, A))

Submission status: Accepted

Solution code:
import sys
from collections import deque


def input(): return sys.stdin.readline().strip()


L = []
T = deque()
A = deque()
left = 0
total = 0

s = input()
for i, c in enumerate(s):
    if c == '\\':
        T.append(i)
    elif c == '/':
        if len(T) == 0:
            continue
        if i == 0:
            continue
        left = T.pop()
        area = (left, i - left)
        A.append(area)
        total += area[1]

while len(A) > 0:
    if len(A) == 1:
        a1 = A.pop()
        L.append(str(a1[1]))
        break
    a1 = A.pop()
    a2 = A.pop()
    if a1[0] < a2[0]:
        A.append((a1[0], a1[1] + a2[1]))
    else:
        L.append(str(a1[1]))
        A.append(a2)

print(total)
L.reverse()
print((str(len(L)) + ' ' + ' '.join(L)).strip())

Submission status: Accepted

Solution code:
total_stack = []
single_stack = []

def main():
    input_line = input()
    total = 0

    for i,s in enumerate(input_line):
        if s == "\\":
            total_stack.append(i)
        elif s == "/" and len(total_stack) != 0:
            a = total_stack.pop()
            total += i - a
            area = 0
            while len(single_stack) != 0 and a < single_stack[-1][0]:
                area += single_stack.pop()[1]
            single_stack.append([a, area + i - a])

    print(total)
    print(' '.join([str(len(single_stack))] + [str(i[1]) for i in single_stack]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
A = []
down = [] # index
Asum = 0

for i, line in enumerate(S):
    if line == "\\":  # "\"
        down.append(i)
    elif line == "/" and down:
        j = down.pop()
        temp = i - j
        Asum += temp
        while A and A[-1][0] > j:
            temp += A.pop()[1]
        A.append([j, temp])

print(Asum)
print(len(A), *[S for i, S in A])

Submission status: Accepted

Solution code:
from enum import Enum
import sys
import math
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
 
class Info:
    def __init__(self,arg_start,arg_end,arg_S):
        self.start = arg_start
        self.end = arg_end
        self.S = arg_S
 
 
LOC = []
POOL = []
 
line = input()
loc = 0
sum_S = 0
 
for ch in line:
 
    if ch == '\\':
 
        LOC.append(loc)
 
    elif ch == '/':
 
        if len(LOC) == 0:
            continue
 
        tmp_start = int(LOC.pop())
        tmp_end = loc
        tmp_S = tmp_end-tmp_start;
        sum_S += tmp_S;
 
        #既に突っ込まれている池の断片が、今回の断片の部分区間になるなら統合する
        while len(POOL) > 0:
            if POOL[-1].start > tmp_start and POOL[-1].end < tmp_end:
                tmp_S += POOL[-1].S
                POOL.pop()
            else:
                break
 
        POOL.append(Info(tmp_start,tmp_end,tmp_S))
 
    else:
        pass
 
    loc += 1
 
print("%d"%(sum_S))
print("%d"%(len(POOL)),end = "")
 
while len(POOL) > 0:
    print(" %d"%(POOL[0].S),end = "") #先頭から
    POOL.pop(0)
 
print()

Submission status: Accepted

Solution code:
from collections import deque


def main():
    sequence = input()
    stack = deque()
    pools = deque()
    for i, e in enumerate(sequence):
        if e == '\\':
            stack.append(i)
        elif e == '_':
            continue
        else:
            if len(stack) == 0:
                continue
            pre = stack.pop()
            s = 0
            while len(pools) > 0 and pre < pools[-1][1]:
                tmp = pools.pop()
                s += tmp[0]
            pools.append((s + i - pre, i))
    length = str(len(pools))
    results = list(map(lambda x: x[0], pools))
    print(sum(results))
    print(' '.join([length] + [str(r) for r in results]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from typing import List


class Info:
    def __init__(self, arg_start, arg_end, arg_area):
        self.start = arg_start
        self.end = arg_end
        self.area = arg_area


if __name__ == "__main__":
    LOC: List[int] = []
    POOL: List[Info] = []
    all_symbols = input()
    loc = 0
    total_area = 0

    for symbol in all_symbols:
        if symbol == '\\':
            LOC.append(loc)

        elif symbol == '/':
            if len(LOC) == 0:
                continue

            tmp_start = int(LOC.pop())
            tmp_end = loc
            tmp_area = tmp_end - tmp_start
            total_area += tmp_area

            while len(POOL) > 0:
                # \  / : (tmp_start, tmp_end)
                #  \/ : (POOL[-1].start, POOL[-1].end)
                if tmp_start < POOL[-1].start and POOL[-1].end < tmp_end:
                    tmp_area += POOL[-1].area
                    POOL.pop()
                else:
                    break

            POOL.append(Info(tmp_start, tmp_end, tmp_area))

        else:
            pass

        loc += 1

    print(f"{total_area}")
    print(f"{len(POOL)}", end="")
    while len(POOL) > 0:
        print(f" {POOL[0].area}", end="")
        POOL.pop(0)
    print()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    s1 = []
    s2 = []

    data = input()
    total_sum = 0
    for i in range(len(data)):
        if data[i] == '\\':
            s1.append(i)
        elif data[i] == '/' and len(s1) > 0:
            j = s1.pop()
            a = i - j
            total_sum += a

            while (len(s2) and s2[-1][0] > j):
                temp = s2.pop()
                a += temp[1]
            s2.append([j, a])

    print(total_sum)
    length = len(s2)
    if not length:
        print(length)
    else:
        print(length, end=' ')
    for i in range(0, length):
        endl = ' '
        if i == length - 1:
            endl = '\n'
        print(s2[i][1], end=endl)

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    s=[]#"\\"のindexが入るスタック
    p=[]#popしたindexと面積のタプルが入るスタック
    i=0#index番号
    a=0#
    for c in input():
        if "\\"==c:#"\"だと認識できない仕様
            s+=[i]#index番号の取得とスタックへの挿入
        elif "/" ==c and s:#index番号が空の時はやらない
            j=s.pop()#末尾のindex番号を取得して削除
            t=i-j
            a+=t
            while p and p[-1][0]>j:
                t+=p[-1][1];
                p.pop()
            p+=[(j,t)]
        i+=1
    print(a)
    if p:
        print(len(p),*list(zip(*p))[1])
    else:
        print(0)

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin
data = list(stdin.readline().rstrip())
i_stack, a_stack, b_stack = deque(), deque(), deque()
ans = 0
for i, c in enumerate(data):
    if c == '\\':
        i_stack.append(i)
    elif c == '/' and len(i_stack) != 0:
        j = i_stack.pop()
        diff = i - j
        ans += diff 
        while a_stack and a_stack[-1] > j:
            diff += b_stack.pop()
            a_stack.pop()
        a_stack.append(j)
        b_stack.append(diff)
print(ans)
print(len(a_stack), *b_stack)

Submission status: Accepted

Solution code:

class Stack:
    def __init__(self, n):
        self.A = [0 for i in range(n)]
        self.len = n
        self.top = 0

    def is_empty(self):
        return self.top == 0

    def is_full(self):
        return self.top == self.len - 1

    def push(self, x):
        if not self.is_full():
            self.top += 1
            self.A[self.top] = x

    def pop(self):
        if not self.is_empty():
            x = self.A[self.top]
            self.top -= 1
            return x

    def top_element(self):
        if not self.is_empty():
            return self.A[self.top]


if __name__ == '__main__':
    S1 = Stack(20005)  # for \
    S2 = Stack(10005)  # for ponds
    line = raw_input()
    for i in range(len(line)):
        if line[i] == '\\':
            S1.push(i)
        elif line[i] == '/':
            if S1.is_empty():
                continue
            j = S1.pop()
            cnt = 0  # total area of ponds that can be merged
            # no more ponds can be merged if S2.top_element()[0] < j, why?
            # because '/' is clearly increasing toward the stack top
            # and '\' is increasing toward the stack top, too
            # if not, there exists a pond that should have been merged
            while not S2.is_empty() and S2.top_element()[0] > j:
                cnt += S2.pop()[1]
            S2.push((j, cnt + i - j))
        else:
            pass

    l = []
    cnt = 0
    while not S2.is_empty():
        x = S2.pop()
        cnt += x[1]
        l.append(x)
    l.reverse()
    print cnt
    print len(l),
    for x in l:
        print x[1],
    print

Submission status: Accepted

Solution code:
from enum import Enum
from queue import Queue
import collections

import sys
import math

class Info:
    def __init__(self,arg_start,arg_end,arg_S):
        self.start = arg_start
        self.end = arg_end
        self.S = arg_S

LOC=[]
POOL=[]

line = input()
loc = 0
sum_S = 0

for ch in line:
    if ch == '\\':
        LOC.append(loc)
    elif ch == '/':
        if len(LOC) ==0:
            continue
        tmp_start = int(LOC.pop())
        tmp_end = loc
        tmp_S = tmp_end-tmp_start
        sum_S += tmp_S

        while len(POOL) > 0:
            if POOL[-1].start > tmp_start and POOL[-1].end < tmp_end:
                tmp_S += POOL[-1].S
                POOL.pop()
            else:
                break
        POOL.append(Info(tmp_start,tmp_end,tmp_S))

    else:
        pass

    loc += 1
print("%d"%(sum_S))
print("%d"%(len(POOL)),end = "")

while len(POOL) > 0:
    print(" %d"%(POOL[0].S),end = "") #先頭から
    POOL.pop(0)

print()

Submission status: Accepted

Solution code:
input_line = input()
stack = []
area = 0
pond_area = 0
pond_areas = []

for i,s in enumerate(input_line):
    if s == '\\':
        stack.append(i)
        if pond_area: 
            pond_areas.append((l, pond_area))
            pond_area = 0
    elif stack and s == '/':
        l = stack.pop()
        area += i - l
        while True:
            if not pond_areas or pond_areas[-1][0] < l: break
            else: 
                pond_area += pond_areas.pop()[1]
        pond_area += i-l

if pond_area > 0: pond_areas.append((l, pond_area))

print(area)
print( " ".join([str(len(pond_areas))] + [str(a[1]) for a in pond_areas]) )

Submission status: Accepted

Solution code:
sectional_view = input()

list_index_down = []
list_index_area_left = []
list_area = []


for index_now, mark in enumerate(sectional_view):
    if   mark == "\\":
        list_index_down += [index_now]
    elif mark == "/" and list_index_down:
        index_down_last = list_index_down.pop()
        area = index_now - index_down_last

        while ((list_index_area_left) and
               (list_index_area_left[-1] > index_down_last)):
            area += list_area.pop()
            list_index_area_left.pop()

        list_index_area_left.append(index_down_last)
        list_area.append(area)


print(sum(list_area))
print(len(list_area),*(list_area))

Submission status: Accepted

Solution code:
from collections import deque


def main():
    total = 0
    s1 = deque()
    s2 = deque()
    str = input()
    for i in range(len(str)):
        if str[i] == "\\":
            s1.append(i)
        elif len(s1) > 0 and str[i] == "/":
            old = s1.pop()
            tmp = 0
            while len(s2) > 0 and old < s2[-1][0]:
                tmp += s2.pop()[1]
            s2.append([old, tmp + i - old])

    for i in range(len(s2)):
        total += s2[i][1]
    print(total)
    if s2:
        print(len(s2), end="")
        for i in range(len(s2)):
            if i == len(s2) - 1:
               print(" {}".format(s2[i][1]))
            else:
               print(" {}".format(s2[i][1]), end="")
    else:
        print(len(s2))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = []
p = []
a = i = 0
for c in input():
	if c == "\\":
		s += [i]
	elif c == "/" and s:
		j = s.pop()
		t = i-j
		a += t
		while p and p[-1][0] > j:
			t += p[-1][1]
			p.pop()
		p += [(j,t)]
	i += 1

print(a)
if p:
	print(len(p),*list(zip(*p))[1])
else:
	print(0)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
n = len(s)
stack1 = deque()
stack2 = deque()
A = 0
a = 0
for ni in range(n):
    if s[ni] == "\\":
        stack1.append(ni)
    elif s[ni] == "/" and len(stack1) > 0 :
        tmp = stack1.pop()
        A += ni - tmp
        while True:
            if len(stack2) > 0 and stack2[-1][0] > tmp:
                a += stack2.pop()[1]
            else:
                break
        a += ni - tmp
        stack2.append([ni,a])
        a = 0
print(A)
print(len(stack2),*[i[1] for i in stack2])

Submission status: Accepted

Solution code:
def _main():
  text = list(input())

  S1 = []
  S2 = []
  area = 0

  for i in range(len(text)):
    if text[i] == '\\':
      S1.append(i)
    elif text[i] == '/' and len(S1) > 0:
      j = S1.pop()
      section_area = i - j
      area += section_area
      while len(S2) > 0 and S2[-1][0] > j:
        section_area += S2[-1][1]
        S2.pop()
      S2.append([j, section_area])
  print(area)
  print(len(S2), *[n[1] for n in S2])




if __name__ == '__main__':
  _main()

Submission status: Accepted

Solution code:
lines = list(input())
total = 0
size = 0

stack = []
ponds = []
size = 0
for i in range(len(lines)):
    if lines[i] == "\\":
        stack.append(i)
    elif lines[i] == "/":
        if not stack == []:
            left_index = stack.pop()
            size = i - left_index
            target_index = left_index
            
            #order is tricky
            while not ponds == [] and ponds[-1][0] > left_index:
                direct = ponds.pop()
                target_index = direct[0]
                size += direct[1]
            ponds.append([target_index,size])




ans = []
ans.append(len(ponds))
for _ in ponds:
    ans.append(_[1])

print(sum(ans[1:]))
print(" ".join(map(str,ans)))

Submission status: Accepted

Solution code:
s = input()

sum = 0
process_stack = []
puddle_stack = []

length = len(s)

for i in range(length):
    if s[i] == "\\":
        process_stack.append(i)
    elif s[i] == "/":
        if process_stack:
            edge = process_stack.pop()
            sum += i - edge
            puddle_sum = 0
            while puddle_stack:
                if puddle_stack[len(puddle_stack) - 1][0] < edge:
                    break;
                puddle_sum += puddle_stack.pop()[1]
            puddle_stack.append((edge, puddle_sum + i - edge))

print(sum)

result_string = ""
puddle_num = len(puddle_stack)
result_string += str(puddle_num)
for i in range(len(puddle_stack)):
    result_string += " "
    result_string += str(puddle_stack.pop(0)[1])

print(result_string)

Submission status: Accepted

Solution code:
s=input()
stack=[]
tmpans=[]
ans=[]
for k,v in enumerate(s):
    if v=="\\":
        stack.append(k)
    elif v=="/" and len(stack)>0:
        p=stack.pop()
        a=k-p
        while len(tmpans)>0 and tmpans[-1][-1]>p:
            a+=tmpans[-1][0]
            tmpans.pop()
        tmpans.append([a,p])
x=[i[0] for i in tmpans]
print(sum(x))
print(len(x),*x)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
S1 = deque()
S2 = deque()
total = 0
for i in range(len(s)):
    if s[i] == '\\':
        S1.append(i)
    elif s[i] == '/':
        if len(S1) != 0:
            a = S1.pop()
            total += i - a
            area = i - a
            if len(S2) != 0:
                while S2[-1][0] > a:
                    area += S2.pop()[1]
                    if len(S2) == 0:
                        break
                S2.append([a ,area])
            else:
                S2.append([a, area])
        else:
            pass
print(total)
if total == 0:
    print(0)
else:
    print(len(S2), ' '.join(map(lambda x: str(x[1]), S2)))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline

    from collections import deque

    s = input()
    s1 = deque([])
    s2 = deque([])
    sum_ = 0    # sumを変数名にすると、関数のsum()を使えなくなるので注意
    for i, ch in enumerate(s):
        if ch == '\\':   # \はエスケープ文字として機能してしまうので２連続で書く必要がある
            s1.append(i)
        elif ch == '/' and len(s1) > 0:
            j = s1.pop()
            sum_ += i - j
            a = i - j
            while len(s2) > 0 and s2[-1][0] > j:
                a += s2.pop()[1]
            s2.append([j,a])

    ans = []
    while len(s2) > 0:
        ans.append(s2.pop()[1])    # s2から水たまりの面積だけを取り出してansに入れる
    ans.reverse()                  # 順番反転
    print(sum(ans))
    ans.insert(0,len(ans))   # 先頭に水たまりの個数を挿入
    print(' '.join(map(str, ans)))

Submission status: Accepted

Solution code:
f = list(input())
s = []

for i, v in enumerate(f):
    if v == '\\':
        s.append([v, i])
    elif v == '/' and len(s) > 0:
        n = s.pop()
        if n[0] == '\\':
            t = i-n[1]
            s.append(['.', t])
        elif len(s) > 0:
            j = len(s)-1
            n1 = n[1]
            while j >= 0 and s[j][0] == '.':
                #print(n1, s[j][1])
                n1 += s[j][1]
                j -= 1
            if j >= 0:
                m = s[j]
                # print(m)
                n1 += i-m[1]
                s = s[:j]
                s.append(['.', n1])
            else:
                # print("err")
                s.append(n)

        else:
            s.append(n)
    #print(v, s, i)


a = [v[1] for v in s if v[0] == '.']
print(sum(a))
a.insert(0, len(a))
print(" ".join([str(v) for v in a]))

Submission status: Accepted

Solution code:
S = input()
lake_sum = 0
stack1 = []
stack2 = []
for i in range(len(S)):
    if S[i] == '\\':
        stack1.append(i)
    elif S[i] == '/':
        if stack1 == []:
            continue
        j = stack1.pop()
        area = i - j
        lake_sum += area
        while True:
            if stack2 == []:
                break
            k, l = stack2.pop()
            if k < j:
                stack2.append((k, l))
                break
            area += l
        stack2.append((j, area))
print(lake_sum)
if stack2 == []:
    print(0)
else:
    lakes = []
    lakes.append(str(len(stack2)))
    for lake in stack2:
        lakes.append(str(lake[1]))
    print(' '.join(lakes))

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline

def putsarray(array):
    if len(array) == 0:
        print()
        return
    for i in range(len(array) - 1):
        print(int(array[i]), end=' ')
    print(int(array[-1]))

def main():
    stack = deque([])
    instr = input()
    is_same_lake = False
    result = 0

    lakes = deque([])

    for i in range(len(instr)):
        char = instr[i]
        if char == "\\":
            stack.append(i)
        elif char == "_":
            pass
        elif char == "/":
            if len(stack) == 0:
                is_same_lake = False
                continue
            down = stack.pop()
            result += i - down
            lake_area = i - down
            index = len(lakes) -1
            while True:
                if index < 0:
                    break
                lake = lakes[index]
                if down < lake[0] and lake[1] < i:
                    lake_area += lakes.pop()[2]
                else:
                    break
                index -= 1
                    
            lakes.append((down, i, lake_area))

    print(int(result))
    putsarray([len(lakes),] + [l[2] for l in lakes])

    

main()

Submission status: Accepted

Solution code:
l = input()
l += "_"
stack1 = []
stack2 = []
A = 0
for i in range(len(l)):
    if l[i] == "\\":
        stack1.append(i)
    elif l[i] == "/" and stack1:
        b = stack1.pop()
        a = i - b
        A += a
        while stack2 and stack2[-1][0] > b:
            a += stack2.pop()[1]
        stack2.append([b, a])
print(A)
print(len(stack2), *(i[1] for i in stack2))

Submission status: Accepted

Solution code:
def hightMapGenerator(text):
    height_map = []
    current_height = 0
    for letter in text:
        if letter == '\\':
            current_height -= 1
        elif letter == '/':
            current_height += 1
        else :
            pass
        height_map.append(current_height)
    return height_map

def pondChecker(text, height_map):
    # 判定
    all_ponds = []
    pond = []
    for n, letter in enumerate(text):
        if pond == []: # 池オブジェクトが空の場合
            if letter == '\\': # かつ、文字列がa の場合
                max_height = max(height_map[n:])
                if height_map[n] + 1 <= max_height: # 現在地よりも、高度の高い位置があるならば
                    start_index = n
                    pond.append(start_index) # スタート位置としてインデックスする
                else :
                    pass
        else : # 池オブジェクとにスタートが入っている場合
            if height_map[start_index] + 1 == height_map[n]: # スタートインデックスと同じ高さに来たら終わりをインデックスして、初期化
                end_index = n
                pond.append(end_index)
                all_ponds.append(pond)
                pond = []
    return all_ponds
    
def squareCalc(text, pond_height_map):
    square = 0
    current_height = max(pond_height_map)
    for n, letter in enumerate(text):
        if letter == '\\':
            square += current_height - pond_height_map[n] - 0.5
        elif letter == '/':
            square += current_height - pond_height_map[n] + 0.5
        else :
            square += current_height - pond_height_map[n]
    return int(square)

def slicedPondsGenerator(text, height_map, all_ponds):
    sliced_ponds_str = []
    sliced_height_map = []
    for pond in all_ponds:
        start = pond[0]
        end = pond[1] + 1
        sliced_pond_str = text[start:end]
        sliced_height = height_map[start:end]
        sliced_ponds_str.append(sliced_pond_str)
        sliced_height_map.append(sliced_height)
    return sliced_ponds_str, sliced_height_map

text = input()

if '/' not in text:
    print(0)
    print(0)
elif '\\' not in text:
    print(0)
    print(0)
else :
    height_map = hightMapGenerator(text)
    # print(height_map)
    all_ponds = pondChecker(text, height_map)
    if all_ponds == []:
        print(0)
        print(0)
    else :
        # print(all_ponds)
        sliced_ponds_str, sliced_height_map = slicedPondsGenerator(text, height_map, all_ponds)
        # print(ponds_str)
        # print(sliced_height_map)
        squares = []
        for n, pond_str in enumerate(sliced_ponds_str):
            square = squareCalc(pond_str, sliced_height_map[n])
            squares.append(square)

        size_of_ponds = sum(squares)
        num_of_ponds = len(squares)
        output = ' '.join([str(n) for n in squares])
        print(size_of_ponds)
        print(num_of_ponds,output)

Submission status: Accepted

Solution code:
# coding: utf-8
s = list(input().rstrip())
ht = []
ponds=[]
for i, ch in enumerate(s):
    if ch == "\\":
        ht.append(i)
    elif ch == "/":
        if ht:
            b = ht.pop()
            ap = i - b
            if not ponds:
                ponds.append([b, ap])
            else:
                while ponds:
                    p = ponds.pop()
                    if p[0] > b:
                        ap += p[1]
                    else:
                        ponds.append([p[0],p[1]])
                        break
                ponds.append([b,ap])
    else:
        pass

ans = ""
area = 0
for point, ar in ponds:
    area += ar
    ans += " "
    ans += str(ar)
print(area)
print(str(len(ponds)) + ans)

Submission status: Accepted

Solution code:
from collections import defaultdict




BS, SL = '\\', '/'

diagram = list(input())

flood = defaultdict(int)
total = 0
stack = []

for cur, terrain in enumerate(diagram):
    if terrain == BS:
        stack.append(cur)
    elif terrain == SL and stack:
        first = stack.pop()
        water = cur-first
        total += water
        for (f_first, f_last) in list(flood):
            if first < f_first < cur:
                flood[(first, cur)] += flood.pop((f_first, f_last))
        flood[(first, cur)] += water

print(total)
print(' '.join(map(str, [len(flood)] + list(flood.values()))))

Submission status: Accepted

Solution code:
from collections import deque

list = list(input())

queue = deque([]) 
queue2 = []
total = 0

for i in range(len(list)):
        if list[i] == '\\':
                queue.append(i)
        elif list[i] == '/' and len(queue) > 0:
                j = queue.pop()
                total += (i - j)
                temp = i - j
                while len(queue2) > 0 and queue2[len(queue2) - 1][0] > j:
                        temp += queue2[len(queue2) - 1][1]
                        queue2.pop()
                queue2.append([j, temp])
strResult = [str(len(queue2))]
for i in range(len(queue2)):
        strResult.append(str(queue2[i][1]))

print(total)
print(" ".join(strResult))

Submission status: Accepted

Solution code:
class Pond:
    def __init__(self, arg_left, arg_area):
        self.left = arg_left
        self.area = arg_area

form = input()
stack = []
stack2 = []
totalArea = 0
for i in range(len(form)):
    #'/'以外なら積む
    tmpForm = form[i]
    if tmpForm == '\\':
        stack.append(i)
    #'/'なら直前の\を探しに行く
    elif tmpForm == '/' and len(stack) > 0:
        xLeft = stack.pop()
        xRight = i
        tmpArea = xRight - xLeft
        totalArea += tmpArea

        #池が連続する場合の処理
        while len(stack2) > 0 and stack2[-1].left > xLeft:
            tmpArea += stack2.pop().area

        stack2.append(Pond(xLeft, tmpArea)) #新たな池を登録

print(totalArea)
ans = []
ans.append(len(stack2))
for pond in stack2:
    ans.append(pond.area)
print(' '.join(map(str, ans)))

Submission status: Accepted

Solution code:
import sys,itertools
from collections import deque

input = sys.stdin.readline


def _main():
    IN = input()
    area = 0
    all_area_stack = deque([])
    local_spot_stack = deque([])
    for i, op in enumerate(IN):
        if(op == '\\'):
            all_area_stack.append(i)
        elif(op == '/' and len(all_area_stack) > 0):
            i_p = all_area_stack.pop()
            area += (i - i_p)
            local_area = i - i_p
            while(len(local_spot_stack) > 0 and local_spot_stack[-1][0] > i_p):
                top_i, top_area = local_spot_stack.pop()
                local_area += top_area
            local_spot_stack.append((i, local_area))
    
    spots = [str(spot[1]) for spot in local_spot_stack]
    str_spots = ' '.join(spots)
    print(area)
    if(len(local_spot_stack) > 0):
        print('{} {}'.format(len(local_spot_stack), str_spots))
    else:
        print(0)





if __name__ == "__main__":
    _main()