Problem:
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 (Python):
import queue

inpstr = input()
stack = queue.LifoQueue()
stack_areas = queue.LifoQueue()

cnt = 0  
area = 0  
for s in inpstr:
	if s == '\\':
		stack.put(cnt)
	elif s == '/':
		if not stack.empty():
			i = stack.get()
			area = area + cnt - i
			
			temp = [i, cnt - i]  
			if stack_areas.empty():
				stack_areas.put(temp)
			else:
				for j in range(0, stack_areas.qsize()):
					temp2 = stack_areas.get()
					if i > temp2[0]:
						stack_areas.put(temp2)
						break
					else:
						temp[1] = temp[1] + temp2[1]
				stack_areas.put(temp)
	elif s == '_':
		pass  
	cnt = cnt + 1

print(area)

areas = []
while not stack_areas.empty():
	areas.append(stack_areas.get()[1])

areas.append(len(areas))
areas.reverse()
print(' '.join([str(n) for n in areas]))

Solution (Python):
from collections import deque
import sys


def func(S):
    i = 0
    water = 0
    stack = []
    waters = []
    while i < len(S):
        for inc, s in enumerate(S[i:]):
            if s == '\\':
                stack.append(s)
                water += len(stack)

            if not stack:
                break

            if s == '/':
                stack.pop()
                water += len(stack)

            if s == '_':
                water += len(stack)

            if not stack:
                break
        if water != 0 and not stack:
            waters.append(water)
        water = 0

        if inc == len(S[i:]) - 1:
            if not stack:
                i = len(S)
            break

        i += inc if inc else 1

    return waters, S[i:]


if __name__ == '__main__':
    S = [c for c in input()]

    waters, S = func(S)

    rev_S = []
    for s in S[::-1]:
        if s == '\\':
            rev_S.append('/')
        elif s == '/':
            rev_S.append('\\')
        else:
            rev_S.append('_')

    new_waters, _ = func(rev_S)
    waters += new_waters[::-1]

print(sum(waters))
print(len(waters), *waters)

Solution (Python):
current_l = 0
stack_s = []
stack_each_area = []
areas = []
for s in input():

    if(s == '\\'):
        stack_s.append(current_l)
    elif(s == '/'):
        if(len(stack_s)==0):
            continue
        left_edge = stack_s.pop(-1)
        area = current_l - left_edge
        areas.append(area)
        
        merged_area = area
        merged_j = []
        for j in range(len(stack_each_area)):
            if( left_edge < stack_each_area[j][0] and stack_each_area[j][0] < current_l):
                merged_area += stack_each_area[j][1]
                merged_j.append(j)
        for del_j in merged_j[::-1]:
            del stack_each_area[del_j]
        stack_each_area.append((current_l, merged_area))
    current_l += 1
print(sum(areas))
if(len(stack_each_area) != 0):
    print(len(stack_each_area), " ".join([ str(lm[1]) for lm in stack_each_area]))
else:
    print(0)

Solution (Python):

if __name__ == "__main__":
    line = input()

    S1 = []
    S2 = []
    total = 0

    for k, v in enumerate(line):
        if v == '\\':
            S1.append(k)
        elif v == '/' and len(S1) > 0:
            left_pos = S1.pop()
            area = k - left_pos
            total += area

            while True:
                if len(S2) < 1 or S2[-1][0] < left_pos:
                    S2.append([left_pos, area])
                    break
                else:
                    btm_area = S2.pop()
                    area += btm_area[1]

    print(total)
    indiv = [str(v[1]) for v in S2]
    indiv.insert(0, str(len(S2)))
    print(' '.join(indiv))

Solution (Python):
from sys import stdin
from collections import deque

def main():
    point = 0
    watermarks = []
    ponds = []

    for mark in list(stdin.readline()):
        point += 1
        if mark == "\\":
            watermarks.append(point)
        elif mark == "/":
            if watermarks:
                start_point = watermarks.pop()
                area = point - start_point
                while ponds and ponds[-1][0] > start_point:
                    area += ponds.pop()[1]
                ponds.append((start_point, area))

    pond_list = [pond[1] for pond in ponds]
    print(sum(pond_list))
    print(len(pond_list), *pond_list)

main()

Solution (Python):
from collections import deque
x = input()
A = 0
S1 = deque()
S2 = deque()

for i in range(len(x)):
    if x[i] == "\\":
        S1.append(i)
    elif x[i] == "/" and len(S1) > 0 :
        j = S1.pop()
        A += i - j
        a  = i - j
        while  len(S2) > 0 and S2[-1][0] > j:
            a += S2[-1][1]
            S2.pop()
        S2.append([j,a])
print(A)

if len(S2)== 0:
    print(0)
else:
    print(len(S2),"",end="")
    for i in range(len(S2)):
        if i == len(S2) - 1:
            print(S2[i][1])
        else:
            print(S2[i][1],"",end="")

Solution (Python):
field = input()


idxes = [] 
v_lakes = [] 

for i, f in enumerate(field):
    if f == '\\':
        idxes.append(i) 
    elif f == '/' and len(idxes) > 0:
        j = idxes.pop() 
        v = i - j   
        
        
        while len(v_lakes) > 0 and v_lakes[-1][0] > j:
            v += v_lakes.pop()[1] 
        v_lakes.append((j, v))

ans = [v[1] for v in v_lakes]
print(sum(ans))
print(len(ans), *ans)

Solution (Python):
from collections import deque

data = input()
tmp = deque()
se = list()
ase = list()
lakes = list()

x = 0

for a in data:
  if a == '\\':
    tmp.append(x)
  if a == '/' and len(tmp) > 0 :
    s = tmp.pop()
    se.append([s,x])
  x += 1
se.sort()


if len(se) > 0 :
  ase.append(se[0])
  for i in range(1,len(se)) :
    s,e = ase[len(ase)-1]
    if e < se[i][0]:
      ase.append(se[i])


for a in ase:
  lsize = 0
  deep = 0
  lx = a[0]
  for m in data[a[0]:a[1] + 1] :
    if m == '\\':
      lsize += 1 + deep
      deep += 1
    elif m == '/':
      deep -= 1
      lsize += deep
      if lx == a[1]:
        lakes.append(lsize)
    else: 
      lsize += deep
    lx += 1

print(sum(lakes))
print(len(lakes),end="")
for lake in lakes:
  print(" {}".format(lake),end="")
print()

Solution (Python):
landform = [c for c in input()]
down = []
areas = []

for i in range(len(landform)):
    if landform[i] == '\\':
        down.append(i)
    if landform[i] == '/':
        if down:
            pair = down.pop()
            area = i - pair
            while areas and areas[-1][0] > pair:
                area += areas.pop()[1]
            areas.append([pair, area])

sum_area = 0
for area in areas:
    sum_area += area[1]
print(sum_area)

ans = []
ans.append(str(len(areas)))
ans += [str(area[1]) for area in areas]

print(' '.join(ans))

Solution (Python):
l = input()

stack1 = []
stack2 = []

all_area = 0

for val in range(len(l)):
    if l[val] == "\\":
        stack1.append(val)
    elif l[val] == "/" and stack1 != []:
        temp = stack1.pop(-1)
        all_area = all_area + (val - temp)
        each_area = val - temp
        while stack2 != [] and stack2[-1][0] > temp:
            each_area = each_area + stack2.pop(-1)[1]

        stack2.append([temp, each_area])
    else:
        pass

print(all_area)
print(len(stack2), end="")

for i in range(len(stack2)):
    print(" ", end="")
    print(stack2[i][1],end="")

print("")

Solution (Python):
S = input()
st = [(0, 0, -1)]
cur = 0; ma = 0
for i, c in enumerate(S):
    if c == '/':
        cur += 1
        s = 0
        while st and st[-1][0] == cur-1 and cur <= ma:
            lev, su, j = st.pop()
            s += su
        if st and st[-1][0] == cur:
            s += i - st[-1][2] - 1
        ma = max(ma, cur)
        st.append((cur, s, i))
    elif c == '\\':
        cur -= 1
        st.append((cur, 0, i))
    elif c == '_':
        st.append((cur, 0, i))
ans = []
for lev, su, j in st:
    if su > 0:
        ans.append(su)
print(sum(ans))
print(len(ans), *ans)

Solution (Python):
s = input()
stack = []
stack1 = []
tot = 0
rs = []
for i in range(len(s)):
    if s[i] == '\\':
        stack.append(i)
    elif s[i] == '/':
        if len(stack) > 0:
            tmp = i - stack[-1]
            tot += tmp
            while len(stack1) > 0 and stack1[-1][0] > stack[-1]:
                tmp += stack1[-1][1]
                stack1.pop()
            stack1.append((stack[-1], tmp))
            stack.pop()
print(tot)
print(len(stack1), *(y for x,y in stack1))

Solution (Python):
from collections import deque

S = input()

pool_q=deque()
slope_q=deque()

for i,s in enumerate(S):
    if s=="\\":
        slope_q.append(i)
        pool_q.append((i,0))
    elif s=="/":
        if len(slope_q) > 0:
            pre_down=slope_q.pop()
            plus=i-pre_down
            while 1:
                pre_pool=pool_q.pop()
                if pre_pool[0] == pre_down:
                    pool_q.append((pre_down,plus))
                    break
                else:
                    plus+=pre_pool[1]
SUM = 0
ans=[0]
cnt=0
for i in range(len(pool_q)):
    pool=pool_q.popleft()[1]
    if pool > 0:
        ans.append(pool)
        SUM+=pool
        cnt+=1
ans[0]=cnt
print(SUM)
print(*ans)

Solution (Python):
S = [s for s in input()]

stack1 = []
stack2 = []

for i in range(len(S)):
    if S[i] == '\\':
        stack1.append(i)
    elif S[i] == '/':
        if len(stack1) != 0:
            pos = stack1.pop()
            area = i - pos
            while len(stack2) != 0:
                pair = stack2.pop()
                if pos < pair[0]:
                    area += pair[1]
                else:
                    stack2.append(pair)
                    break
            stack2.append((pos, area))

print(sum([s[1] for s in stack2]))
print(len(stack2), *[s[1] for s in stack2])

Solution (Python):
from collections import deque
S = input()
d = deque()
d2 = deque()
r = []
r_l = 0
buf = 0
for i, s in enumerate(S):
    i += 1
    if s == '\\':
        d.append(i)
    elif s == '/' and len(d) > 0:
        k = d.pop()
        r_l += i - k
        buf = i - k
        while(len(d2) > 0 and d2[-1][0] > k):
            _d = d2.pop()
            buf += _d[1]
        l = [k, buf]
        d2.append(l)

r = []
for i in d2:
    r.append(i[1])
print(r_l)
print(len(r), *r)

Solution (Python):
def main():
    s = input()
    stack = []
    stack_sum = []
    for i in range(len(s)):
        
        if s[i] == "\\":
            stack.append(i)
        elif s[i] == "/":
            if len(stack) != 0:
                sum_t = 0
                last = stack.pop(-1)
                sum_t += (i-last-1)+1
                if len(stack_sum) != 0:
                    for j in reversed(range(len(stack_sum))):
                        if stack_sum[j][0] > last:
                            sum_t += stack_sum[j][1]
                            stack_sum.pop(j)
                stack_sum.append((last, sum_t))
    sum_ans = 0
    ans = []
    for i in range(len(stack_sum)):
        sum_ans +=stack_sum[i][1]
        ans.append(stack_sum[i][1])

    
    
    print(sum(ans))
    print(len(ans),*ans)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

line = input()

s1 = deque()
s2 = deque()

n = len(line)
total_area = 0

for i in range(n):
    if line[i] == "\\":
        s1.append(i)
    elif line[i] == "/":
        if s1: 
            j = s1.pop()
            area = i - j
            total_area += area
            if s2 and j < s2[-1][0]: 
                while s2 and j < s2[-1][0]:
                    pre_pond = s2.pop()[1]
                    area += pre_pond
            s2.append([j,area])

print(total_area) 
if total_area!=0:
    print("{} ".format(len(s2)) + " ".join([str(x[1]) for x in s2]))
else:
    print(0)

Solution (Python):

A = list(input())



sum = 0

Stack1 = [] 
Stack2 = [] 
for i in range(len(A)):
    if A[i] == "\\":
        Stack1.append(i)
    elif A[i] == "/":
        if Stack1 != []:
            ip = Stack1.pop()
            tmp_sum = i-ip
            sum += i-ip

            xp, yp = ip,tmp_sum

            
            if Stack2 == []:
                Stack2.append((i, tmp_sum))
            
            else:

                x, y = Stack2.pop()
                
                while ( x- xp >= 0):
                    yp = y + yp
                    if Stack2 == []:
                        xp = x
                        Stack2.append((xp, yp))
                        break
                    else:
                        x, y = Stack2.pop()
                    
                else:
                    Stack2.append((x, y))
                    Stack2.append((xp, yp))


print(sum)
if sum == 0:
    print(0)
else:
    print(len(Stack2), end=' ') 
    for i in range(len(Stack2)):
        x, y = Stack2[i]
        if i < len(Stack2)-1:
            print(y, end=' ')
        else:
            print(y)

Solution (Python):
chikei = input()
v_lakes = []
idx_stack = []

for i, c in enumerate(chikei):
	if c is "\\":
		idx_stack.append(i)
	elif c is "/" and idx_stack:
		j = idx_stack.pop()
		v = i - j
		while v_lakes and j < v_lakes[-1][0]:
			v += v_lakes.pop()[1]
		v_lakes.append((j, v))

ans = [len(v_lakes)]
v = [v[1] for v in v_lakes]
ans.extend(v)
print(sum(v))
print(*ans)

Solution (Python):
def Main():
    cross = input().replace("\n", "")

    S = list()
    V = list()
    ans = list()

    for i in range(len(cross)):
        r = cross[i]

        if r == "\\":
            S.append(i)
        elif r == "/":
            if len(S) > 0:
                j = S.pop()
                v = i - j
                V.append([v, j])

    while len(V) > 0 :
        total = 0
        [v, j] = V.pop()
        total += v

        while True:
            if len(V) > 0:
                [vv, jj] = V.pop()

                if jj > j:
                    total += vv
                else:
                    V.append([vv, jj])
                    ans.append(total)
                    break
            else:
                ans.append(total)
                break

    print("{}".format(sum(ans)))
    print(len(ans), *reversed(ans))

Main()

Solution (Python):
s=input()
s1=[]
s2=[]
ans=0
for i in range(len(s)):
    if(s[i]=="\\"):
        s1.append(i)
    elif(s[i]=="/" and s1):
        j = s1.pop()
        a=i-j
        ans+=a

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

print(ans)
print(len(s2),*(a for j, a in s2))

Solution (Python):
from sys import stdin
class Stack():
    def __init__(self):
        self.top=0
        self.top2=0
        self.S=[None]*1000
        self.S2= [None] * 1000
        self.total=0
        self.count=0
        self.haihun=0
        self.arealist=[]
        self.N=0
        self.part=0
        self.compt=False
        self.calc_flag=False
        self.up=0
    def isEmpty(self):
        return self.top==0
    def push(self,fig,finish=0):

        if fig=='\\':
            self.count += 1
            self.S.insert(self.top,self.count)
            self.top += 1


        elif fig=='/':
            self.count += 1
            it=self.pop()
            if self.top > 0: self.top -= 1

            if it is not None:

                
                self.S2.insert(self.top2, ([self.count - it, it]))
                self.top2+=1

                while(True):
                    if self.S2[self.top2 - 2] is None:break
                    if self.S2[self.top2-2][1]> it:
                        i=self.pop2()
                        if self.top2 > 0: self.top2 -= 1  
                        j = self.pop2()
                        if self.top2> 0: self.top2 -= 1  
                        self.S2.insert(self.top2,([i[0]+j[0],i[1]]))
                        self.top2 += 1
                    else:break
                if self.pop(flag=1) is None:  
                    self.N += 1  
                    while (True):
                        vari = self.pop2(flag=1)
                        if vari is not None:
                            if it <= vari[1]:
                                self.total += vari[0]
                            else:
                                break
                            self.pop2()
                            if self.top2 > 0: self.top2 -= 1  
                        else:
                            break
                    self.part = self.total - self.part
                    self.arealist.append(self.part)
                    self.part = self.total


        elif fig=='_':
            self.count+=1
        if finish==1:
            if self.pop(flag=1) is not  None:  
                rest=[]
                while(True):
                    part=self.popup()
                    self.up+=1
                    self.top2-=1
                    if  part is not None:
                        self.arealist.append(part[0])
                        self.N += 1  
                        self.total+=part[0]
                    else:break





    def pop(self,flag=0):
        vari=self.S[self.top-1]
        if flag==0:
          self.S[self.top-1]=None
        return vari
    def pop2(self,flag=0):
        vari=self.S2[self.top2-1]
        if flag==0:
          self.S2[self.top2-1]=None
        return vari
    def popup(self):
        vari = self.S2[self.up]
        return vari
    def print(self):
        self.push(None,finish=1)
        self.arealist=self.arealist[::-1]
        self.arealist.append(self.N)
        return self.total,self.arealist
cmd =list(stdin.readline())
Stck=Stack()
for str in cmd:
    Stck.push(str)
total,area=Stck.print()
print(total,end='\n')
print(*area[::-1])

Solution (Python):
sum = 0
stack  = []
stack2 = []

str_inpt = input()

for i in range(len(str_inpt)):
  if str_inpt[i] == '\\':
    stack.append(i)
  elif str_inpt[i] == '/':
    if len(stack) < 1: continue
    pop_index = stack.pop(-1)
    area = i - pop_index
    sum += area
    while True:
      if len(stack2) < 1: break
      if stack2[-1][0] < pop_index: break
      if pop_index < stack2[-1][0]:
        pop_stack2 = stack2.pop(-1)
        area += pop_stack2[-1]

    stack2.append([pop_index, area])

print(sum)

if len(stack2) == 0:
  print('0')
else:
  print(str(len(stack2)) + ' ' + ' '.join([str(e[1]) for e in stack2]))

Solution (Python):
sum = 0
stack  = []
stack2 = []

str_inpt = input()

for i in range(len(str_inpt)):
  if str_inpt[i] == '\\':
    stack.append(i)
  elif str_inpt[i] == '/':
    if len(stack) < 1: continue
    pop_index = stack.pop(-1)
    area = i - pop_index
    sum += area
    while True:
      if len(stack2) < 1: break
      if stack2[-1][0] < pop_index: break
      if pop_index < stack2[-1][0]:
        pop_stack2 = stack2.pop(-1)
        area += pop_stack2[-1]

    stack2.append([pop_index, area])

print(sum)

if len(stack2) == 0:
  print('0')
else:
  print(str(len(stack2)) + ' ' + ' '.join([str(e[1]) for e in stack2]))

Solution (Python):
if __name__ == '__main__':

	l = input()
	S1, S2 = [], []
	
	sum = 0
	n = len(l)
	for i in range(n):
		if l[i] == "\\":
			S1.append(i)
		elif l[i] == "/" and S1:
			j = S1.pop()
			a = i - j
			sum += a
	
			while S2 and S2[-1][0] > j:
				a += S2.pop()[1]
			S2.append([j, a])
	
	print(sum)
	print(len(S2), *(a for j, a in S2))

Solution (Python):
import sys,bisect
sys.setrecursionlimit(15000)
s = sys.stdin.readline().rstrip()
st = []
ars = []
ar = 0
for i,c in enumerate(s):
  if c == "\\":
    st.append(i)
  elif c == "/":
    if st:
      j = st.pop()
      ar += i-j
      while ars and j < ars[-1][1]:
        ar0,_ = ars.pop()
        ar += ar0
      ars.append([ar,j])
      ar = 0
print(sum([ar for ar,_ in ars]))
if len(ars)==0:
  print(0)
else:
  print(len(ars)," ".join(map(str,[ar for ar,_ in ars])))

Solution (Python):



x = input()
lst = list(x)
pt = []
rst = []
k = 0


cnt = 0
tmp = ""
for s in lst:
    if s == "/":
        if tmp == "_" or tmp == "/":
            cnt += 1
    elif s == "\\":
        if tmp == "\\":
            cnt -= 1
    else:
        if tmp == "\\":
            cnt -= 1

    pt += [cnt]
    tmp = s


i,xs,m2 = 0,0,0
for s in lst:
    if s == "/":
        if i == xs and xs > 0:
            rst += [m2]
            k += 1
            xs,m2 = 0,0
    elif s == "_":
        pass
    else:
        try:
            x = pt[i+1:].index(pt[i])+i+1
            m2 += x-i
            if xs < x:
                xs = x
        except:
            pass
    i += 1
           
print(sum(rst))
print(k,*rst)

Solution (Python):
str = list(input())

a = []
all = 0
areas = []
for i in range(len(str)):
    if str[i] == "\\":
        a.append(i)
    elif str[i] == "/" and len(a) > 0:
        j = a.pop()
        b = i - j
        all += b
        while(len(areas) > 0 and areas[-1][0] > j):
            b += areas.pop()[1]
        areas.append([j, b])

print(int(all))
if len(areas) > 0:
    print(len(areas), *[areas[i][1] for i in range(len(areas))])
else:
    print(0)

Solution (Python):
mountains = list(input())
stack1, stack2 = [], []

for i, grad in enumerate(mountains):
    if grad == '\\':
        stack1.append(i)
    elif grad == '/':
        if stack1:
          left = stack1.pop(-1)
          right = i
          space = right - left
          while stack2 and stack2[-1][0] > left:
            space += stack2.pop(-1)[1]
          stack2.append([left, space])
    else:
        pass

space_list = [space for index, space in stack2]
print(sum(space_list))
print(len(space_list), *space_list)

Solution (Python):
from collections import deque

S = input()


s1 = deque()
s2 = deque()
total = 0

for i, s in enumerate(S):
    if s == '\\':
        s1.append(i)
    elif s == '/':
        if not s1:
            continue
        j = s1.pop()
        total += i - j

        vol = i - j
        while s2 and s2[-1][0] >= j:
            vol += s2.pop()[1]

        s2.append((j, vol))

print(total)
print(len(s2), *[vol for idx, vol in s2])

Solution (Python):
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()

Solution (Python):
S = input()

l1 = []
l2 = []
total_area = 0
for i, j in enumerate(S):
    if j == '\\':
        l1.append(i)
    elif j == '/' and l1:
        i_p = l1.pop()
        v = i -i_p
        total_area += v
    
        while l2 and l2[-1][0] > i_p:
            v += l2.pop()[1]
        l2.append([i_p, v])
        
ans = [str(len(l2))] + [str(k[1]) for k in l2]
print(total_area)
print(' '.join(ans))

Solution (Python):
S = list(input())
areas = 0
cnt = 0
stack_1 = []
stack_2 = []

for i in range(len(S)):
    if S[i] == '\\':
        stack_1.append(i)
    elif S[i] == '/' and len(stack_1) > 0:
        j = stack_1.pop()
        area = i - j
        areas += area
        while len(stack_2) > 0 and stack_2[-1][0] > j:
            area += stack_2.pop()[1]
        stack_2.append([j, area])

print(areas)
if stack_2:
    print(len(stack_2), *list(zip(*stack_2))[1])
else:
    print(0)

Solution (Python):
class Flood:
    """
    begin : int
        水たまりの始まる場所
    area : int
        水たまりの面積
    """
    def __init__(self, begin, area):
        self.begin = begin
        self.area = area

down_index_stack = []  
flood_stack = []  


for i, c in enumerate(input()):
    if c == '\\':
        down_index_stack.append(i)
    if c == '/':
        if len(down_index_stack) >= 1:  

            l = down_index_stack.pop()  
            area = i - l  

            
            
            while len(flood_stack) >= 1 and l < flood_stack[-1].begin:
                
                last_flood = flood_stack.pop()
                area += last_flood.area

            flood_stack.append(Flood(l, area))  


area_list = [flood.area for flood in flood_stack]
print(sum(area_list))
print(' '.join(list(map(str, [len(area_list)] + area_list))))

Solution (Python):



from collections import deque

def make_h_list(slope):
    """
    入力データから高さのリストを作る
    """
    h_list =deque([0])
    for s in slope:
        d = 0
        if s == "\\":
            d = -1
        elif s == "/":
            d = 1
        h_list.append(h_list[-1] + d)
    return h_list

def trim_edge(h_list, left=True):
    """
    水溜りの境界を探す
    """
    if left:
        while h_list:
            h = h_list.popleft()
            if h not in h_list:
                continue
            if h_list[0] - h < 0:
                h_list.appendleft(h)
                return h_list
    else:
        while h_list:
            h = h_list.pop()
            if h not in h_list:
                continue
            if h_list[-1] - h < 0:
                h_list.append(h)
                return h_list
            
def make_w_list(h_list):
    """
    水たまりのリストを作る
    """
    w_list = []
    water = []
    h_list = trim_edge(h_list)
    while h_list:
        h = h_list.popleft()
        if not water:
            water.append(h)
        elif water[0] == h:
            water.append(h)
            w_list.append(water)
            water = []
            h_list.appendleft(h)
            h_list = trim_edge(h_list)
        else:
            water.append(h)
            if water[0] < water[-1]:
                water.pop(0)
    return w_list
                
def calc_area(water):
    """
    面積計算
    """
    h = water[0]
    s = 0
    for i in range(len(water) - 1):
        s += (water[i+1] - h) + (water[i+1] - water[i]) / 2
    return abs(int(s))


in_data = str(input())

h_list = make_h_list(in_data)
h_list = trim_edge(h_list, left=False)

if type(h_list) != None:
    w_list = make_w_list(h_list)
    a_list = [calc_area(water) for water in w_list]
else:
    a_list = []

print(sum(a_list))
print(len(a_list), *a_list)

Solution (Python):
S = input()
left, right, area = [], [], []
for i, s in enumerate(list(S)):
    if s == '\\':
        left.append(i)
    if s == '/' and left != []:
        j = left.pop()
        a = i - j
        while right != [] and j < right[-1]:
            right.pop()
            a += area.pop()
        right.append(j)
        area.append(a)
print(sum(area))
print(len(area), *area)

Solution (Python):
string = input()

s = []
total = 0
s2 = []
for i in range(len(string)):
    if string[i] == '\\':
        s.append(i)
    elif string[i] == '/' and len(s) > 0:
        j = s.pop() 
        area = i - j 
        total += area
        while len(s2) > 0 and s2[-1][0] > j: 
            area += s2[-1][1]
            s2.pop()
        s2.append([j, area]) 

ans = []
for i in range(len(s2)):
    ans.append(str(s2[i][1]))

print(total)
if len(ans) == 0:
    print(len(ans))
else:
    print(len(ans), end = ' ')
    print(' '.join(ans))

Solution (Python):
from collections import deque

T = input()
A = deque()
d = 0
s = 0
for i, token in enumerate(T):
    if token == "\\":
        s += d + 0.5
        if not d:
            l = i
        d += 1
    elif token == "/":
        if not d:
            continue
        d -= 1
        s += d + 0.5
        if not d:
            A.append((l, s))
            s = 0
    else:
        s += d
B = deque()
d = 0
s = 0
for i in range(len(T) - 1, -1, -1):
    token = T[i]
    if token == "/":
        s += d + 0.5
        d += 1
    elif token == "\\":
        if not d:
            continue
        d -= 1
        s += d + 0.5
        if not d:
            B.appendleft((i, s))
            s = 0
    else:
        s += d
S = set(A) | set(B)
S = list(S)
S.sort()
S = [round(s[1]) for s in S]
print(sum(S))
print(len(S), *S)

Solution (Python):
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()

Solution (Python):
from collections import deque

class peke:
    def __init__(self,Start,End,S):
        self.start=Start
        self.end=End
        self.S=S
    

S1=deque()
S2=deque()
total=0
A=input()

for n in range(len(A)):
    if A[n]=='\\':
        S1.append(n)
    elif A[n]=='/' and S1:
        j=S1.pop()
        now=n-j
        total=total+now
        while S2:
            if S2[-1].start>j and S2[-1].end<n:
                now=now+S2[-1].S
                S2.pop()
            else:
                break
        S2.append(peke(j,n,now))

print(total)
print(len(S2),end ="")
while S2:
    print(" ",end="")
    print(S2[0].S,end="")
    S2.popleft()
print()

Solution (Python):
def main():
    S = input()
    stack1, stack2 = [], []
    
    sum = 0
    N = len(S)
    for i in range(N):
        if S[i] == "\\":
            stack1.append(i)
        elif S[i] == "/" and stack1:
            j = stack1.pop()
            tmp = i - j
            sum += tmp
            
            while stack2 and stack2[-1][0] > j:
                tmp += stack2.pop()[1]
            stack2.append([j, tmp])
            
    print(sum)
    print(len(stack2), *[a for j, a in stack2])
    

if __name__=="__main__":
    main()