Problem:


坊主めくりの結末

百人一首の札を使った遊戯の１つに、「坊主めくり」というものがあります。絵札だけを使う簡単な遊戯なので広く楽しまれています。きまりには様々な派生型がありますが、ここで考える坊主めくりはN人の参加者で、以下のようなルールで行います。

 ６４枚の「男」、１５枚の「坊主」、２１枚の「姫」、計１００枚の札を使う。

  絵が見えないように札を裏がえしにしてよく混ぜ、「札山」をつくる。

  参加者の一人目から順番に１枚ずつ札山の札を引く。N人目の次は、また一人目から繰り返す。
      

  引いた札が男なら、引いた人はその札を手に入れる。

  引いた札が坊主なら、引いた人はその札を含め、持っている札をすべて「場」に出す。

  引いた札が姫なら、引いた人はその札を含め、場にある札をすべて手に入れる。

  札山の札がなくなったら終了で、一番多くの札を持っている人の勝ち。

参加者数と札山に積まれた札の順番が与えられたとき、遊戯が終了した時点で各参加者が持っている札数を昇順で並べたものと、場に残っている札数を出力するプログラムを作成してください。

入力

入力は複数のデータセットからなる。入力の終わりはゼロ一つの行で示される。各データセットは以下の形式で与えられる。

N
c1c2 ... c100

各データセットは2行であり、1行目に参加者数を表す整数N(2 ≤ N ≤ 10)が与えられる。続く1行に札山に積まれた札の並びを表す文字列が与えられる。文字ciはi番目に引かれる札を表しており、Mが男、Sが坊主、Lが姫の札を表す。各文字の間に空白は無い。

データセットの数は100を超えない。

出力

データセットごとに、遊戯が終了した時点での札数の並びを１行に出力する。札数の並びとして、各参加者が持っている札の枚数を昇順に並べだものの後に、場に残っている札の枚数を出力する。各札数の間は空白１つで区切る。行の終わりには空白を出力しない。

入力例

2
SSSSSSSSSSSSSSSLLLLLLLLLLLLLLLLLLLLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
2
SSSSSSSSSSSSSSLLLLLLLLLLLLLLLLLLLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMSL
5
MMMMMMSLSLLMMMSMMSLMMMLMMMMLSLLLLMLSMMLMMLLMSSSLMMMMLMLSMLMSMMMMMMMSMMMMMMLMMMMMSMMMLMMLMMMMMMMMMSSM
0

出力例

42 58 0
0 100 0
0 0 3 10 59 28



Sample input:
2
SSSSSSSSSSSSSSSLLLLLLLLLLLLLLLLLLLLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
2
SSSSSSSSSSSSSSLLLLLLLLLLLLLLLLLLLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMSL
5
MMMMMMSLSLLMMMSMMSLMMMLMMMMLSLLLLMLSMMLMMLLMSSSLMMMMLMLSMLMSMMMMMMMSMMMMMMLMMMMMSMMMLMMLMMMMMMMMMSSM
0


Sample output:
42 58 0
0 100 0
0 0 3 10 59 28

Submission status: Wrong Answer

Solution code:
while 1:
    ba = 0

    p = [0 for i in range(int(input()))]
    if len(p) == 0:break

    for i,v in enumerate(list(input())):
        p[i%len(p)] += 1
        if v == 'L':
            p[i%len(p)] += ba
            ba = 0
        elif v== 'S':
            ba += p[i%len(p)] 
            p[i%len(p)] = 0

    result = ''
    for v in p:
       result +=  '%d ' % v
    result += str(ba)
    print(result)

Submission status: Wrong Answer

Solution code:
def ans(n,s):
    p = [[] for i in range(n+1)]
    i = 0
    while True:
        if s == []:
            break
        c = s.pop(0)
        if c == 'M':
            if p[i] == []:
                p[i] = [c]
            else:
                p[i].append(c)
        elif c == 'L':
            if p[i] == []:
                p[i] = [c]
            else:
                p[i].append(c)
            if p[n] != []:
                p[i] += p[n]
            p[n] = []
        elif c == 'S':
            if p[n] == []:
                p[n] = [c]
            else:
                p[n].append(c)
            if p[i] != []:
                p[n] += p[i]
            p[i] = []
        
        i = (i+1) % n
    return p

while True:
    n = int(input())

    if n == 0:
        break
    s = list(input())
    o = ans(n,s)
    oo= list(map(lambda x: str(len(x)),o))
    print(" ".join(oo))

Submission status: Wrong Answer

Solution code:
while 1:
    n = int(input())
    if n == 0:break
    p = [0] * n
    s = 0
    f = input()
    for i in range(100):
        if f[i] == "M":p[i % n] += 1
        elif f[i] == "S":
            s += p[i % n] + 1
            p[i % n] = 0
        else:
            p[i % n] += s + 1
            s = 0
    p += [s]
    print(*p)

Submission status: Wrong Answer

Solution code:
while 1:
	n=input()
	if n==0:break
	a=[0]*n
	c=raw_input()
	b=0
	for i in range(100):
		if c[i]=="M":
			a[i%n]+=1
		elif c[i]=="S":
			b+=a[i%n]+1
			a[i%n]=0
		else:
			a[i%n]+=1+b
			b=0
	print " ".join(map(str,a)),b

Submission status: Wrong Answer

Solution code:
while 1:
	n=input()
	if n==0:break
	a,b,c=[0]*n,0,raw_input()
	for i in range(100):
		if c[i]=="M":
			a[i%n]+=1
		elif c[i]=="S":
			b+=1+a[i%n]
			a[i%n]=0
		else:
			a[i%n]+=1+b
			b=0
	print " ".join(map(str,a)),b

Submission status: Accepted

Solution code:
while 1:
    ba = 0

    p = [0 for i in range(int(input()))]
    if len(p) == 0:break

    for i,v in enumerate(list(input())):
        p[i%len(p)] += 1
        if v == 'L':
            p[i%len(p)] += ba
            ba = 0
        elif v== 'S':
            ba += p[i%len(p)] 
            p[i%len(p)] = 0

    result = ''
    for v in sorted(p):
       result +=  '%d ' % v
    result += str(ba)
    print(result)

Submission status: Accepted

Solution code:
while 1:
  n=int(input())
  if n==0: break
  cs=input()
  a,b=[0]*n,0
  for i in range(100):
    if cs[i]=="M":
      a[i%n]+=1
    elif cs[i]=="S":
      a[i%n],b=0,b+1+a[i%n]
    elif cs[i]=="L":
      a[i%n],b=a[i%n]+1+b,0
  a.sort()
  print(" ".join(map(str,a+[b])))

Submission status: Accepted

Solution code:
while 1:
  n=int(input())
  if n==0: break
  a,b,i=[0]*n,0,0
  for c in input():
    if c=="M": a[i]+=1
    elif c=="S": a[i],b=0,b+1+a[i]
    elif c=="L": a[i],b=a[i]+1+b,0
    i=(i+1)%n
  a.sort()
  print(" ".join(map(str,a+[b])))

Submission status: Accepted

Solution code:
import sys
from itertools import cycle
f = sys.stdin

while True:
    n = int(f.readline())
    if n == 0:
        break
    c, hands, field = f.readline().strip(), [0] * n, 0
    
    for player, ci in zip(cycle(range(n)), c):
        if ci == 'S':
            field += hands[player] + 1
            hands[player] = 0
        elif ci == 'L':
            hands[player] += field + 1
            field = 0
        else:
            hands[player] += 1
    
    hands.sort()
    print(' '.join(map(str, hands)), field)

Submission status: Accepted

Solution code:
while True:
    n = int(raw_input())
    if n == 0: break
    res = [0]*n
    p = 0
    i = 0
    for c in raw_input():
        if c == 'M':
            res[i] += 1
        elif c == 'L':
            res[i] += p+1
            p = 0
        else:
            p += res[i]+1
            res[i] = 0
        i += 1
        if i == n:
            i = 0
    else:
        print ' '.join(map(str, sorted(res))), p

Submission status: Accepted

Solution code:
while True:
    n = input()
    if n==0: break
    s = raw_input()
    ba = 0; p = [0]*n
    for i in range(100):
        if s[i] == 'M':
            p[i%n] += 1
        elif s[i] == 'L':
            p[i%n] += ba+1; ba=0;
        else:
            ba += p[i%n]+1; p[i%n]=0;
    print " ".join(map(str, sorted(p))), str(ba)

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    a,i,c=[0]*n,0,0
    x=input()
    for b in x:
        if b=='S':a[i],c=0,c+a[i]+1
        elif b=='L':a[i],c=a[i]+c+1,0
        else:a[i]+=1
        i=(i+1)%n
    print(' '.join(map(str,sorted(a))),c)

Submission status: Accepted

Solution code:
while True:
    N = int(input())
    if N==0:break
    dataset = input()
    mens = ["" for _ in range(N)]
    i = 0
    p = ""
    while dataset:
        if i == N: i = 0
        card, dataset = dataset[0], dataset[1:]
        if card == "M":
            mens[i] += card
        elif card == "S":
            p += card + mens[i]
            mens[i] = ""
        else:
            mens[i] += card + p
            p = ""
        i += 1
    print(' '.join(map(str, sorted([len(m) for m in mens]))), len(p))

Submission status: Accepted

Solution code:
# Aizu Problem 0280: The Outcome of Bonze

import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


while True:
    n = int(input())
    if n == 0:
        break
    cards = [_ for _ in input().strip()]
    players = [0] * n
    table = 0
    player = 0
    for k in range(100):
        card = cards[k]
        if card == 'M':
            players[player] += 1
        elif card == 'S':
            table += 1 + players[player]
            players[player] = 0
        elif card == 'L':
            players[player] += 1 + table
            table = 0
        player = (player + 1) % n
    players = sorted(players)
    players.append(table)
    print(' '.join([str(p) for p in players]))

Submission status: Accepted

Solution code:
def ans(n,s):
    p = [[] for i in range(n)]
    h = []
    i = 0
    while True:
        if s == []:
            break
        c = s.pop(0)
        if c == 'M':
            if p[i] == []:
                p[i] = [c]
            else:
                p[i].append(c)
        elif c == 'L':
            if p[i] == []:
                p[i] = [c]
            else:
                p[i].append(c)
            if h != []:
                p[i] += h
            h = []
        elif c == 'S':
            if h == []:
                h = [c]
            else:
                h.append(c)
            if p[i] != []:
                h += p[i]
            p[i] = []
        
        i = (i+1) % n
    pp = list(sorted(map(len,p)))
    hh = len(h)
    pp.append(hh)
    
    return " ".join(map(str,pp))

while True:
    n = int(input())
    if n == 0:
        break
    s = list(input())
    o = ans(n,s)
    print(o)

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:break
    p = [0] * n
    s = 0
    f = input()
    for i in range(100):
        if f[i] == "M":p[i % n] += 1
        elif f[i] == "S":
            s += p[i % n] + 1
            p[i % n] = 0
        else:
            p[i % n] += s + 1
            s = 0
    p.sort()
    p += [s]
    print(*p)

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  lst = [0] * n
  ss = input()
  field = 0
  for i in range(100):
    ind = i % n
    if ss[i] == "M":
      lst[ind] += 1
      continue
    if ss[i] == "S":
      field += lst[ind] + 1
      lst[ind] = 0
      continue
    if ss[i] == "L":
      lst[ind] += field + 1
      field = 0
      continue
  lst.sort()
  lst += [field]
  print(*lst)

Submission status: Accepted

Solution code:
while 1:
    n = input()
    if n==0: break
    s = raw_input()
    ba = 0; p = [0]*n;
    for i in xrange(100):
        if s[i]=='M':
            p[i%n]+=1
        elif s[i]=='L':
            p[i%n]+=ba+1; ba=0;
        else:
            ba+=p[i%n]+1; p[i%n]=0;
    print " ".join(map(str,sorted(p))),str(ba)

Submission status: Accepted

Solution code:
while True:
    N = input()
    if N==0: break
    cards = raw_input()
    ba = 0
    player = [0]*N
    for i, card in enumerate(cards):
        pid = i % N
        if card=='S':
            ba += player[pid]+1
            player[pid] = 0
        elif card=='L':
            player[pid] += ba+1
            ba = 0
        elif card=='M':
            player[pid] += 1
    player.sort()
    print ' '.join(map(str, player)), ba

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	a=[0]*n
	c=raw_input()
	b=0
	for i in range(100):
		if c[i]=="M":
			a[i%n]+=1
		elif c[i]=="S":
			b+=1+a[i%n]
			a[i%n]=0
		else:
			a[i%n]+=1+b
			b=0
	print " ".join(map(str,sorted(a))),b

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	a,b,c=[0]*n,0,raw_input()
	for i in range(100):
		if c[i]=="M":
			a[i%n]+=1
		elif c[i]=="S":
			b+=1+a[i%n]
			a[i%n]=0
		else:
			a[i%n]+=1+b
			b=0
	print " ".join(map(str,sorted(a))),b

Submission status: Accepted

Solution code:
n = int(input())
ans = []
while n != 0:
  S = input()
  players = [0 for _ in range(0, n)]
  buf = 0
  for idx, c in enumerate(S, 0):
    target_player = idx % n
    if c == 'S':
      # 坊主
      players[target_player] += 1
      buf += players[target_player]
      players[target_player] = 0
    elif c == 'M':
      # 男
      players[target_player] += 1
    elif c == 'L':
      # 女
      players[target_player] += 1
      players[target_player] += buf
      buf = 0

  players = sorted(players)
  players.append(buf)
  ans.append(players)
  n = int(input())

for players in ans:
  print(' '.join(map(lambda s: str(s), players)))

Submission status: Accepted

Solution code:
while True :
    n = int(input())
    if n == 0 :
        break
    
    card = list(input())
    
    maisu = [0] * (n + 1)
    x = 0
    
    for i in range(100) :
        if card[i] == 'M' :
            maisu[x] += 1
        elif card[i] == 'S' :
            maisu[n] += maisu[x] + 1
            maisu[x] = 0
        elif card[i] == 'L' :
            maisu[x] += maisu[n] + 1
            maisu[n] = 0
            
        x += 1
        if x == n :
            x = 0
    ba = maisu.pop()
    maisu.sort()
    
    for i in range(n) :
        print(maisu[i], end=" ")
    print(ba)

Submission status: Accepted

Solution code:
def play(line, N):
    C = [0] * N
    B = 0

    for i in range(len(line)):
        p = i%N
        if line[i] == 'M':
            C[p] += 1
        elif line[i] == 'S':
            C[p] += 1
            B += C[p]
            C[p] = 0
        elif line[i] == 'L':
            C[p] += B + 1
            B = 0

    C.sort();

    for c in C: print(c, end=" ")
    print(B)

while True:
    N = int(input())
    if N == 0: break
    line = input()
    play(line, N)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    player = [0 for i in range(n)]
    cards = list(input())
    g = 0
    for i in range(100):
        if cards[i] == "M":
            player[i%n] += 1
        elif cards[i] == "S":
            g += player[i%n]+1
            player[i%n] = 0
        else:
            player[i%n] += g+1
            g = 0
    player.sort()
    for p in player:
        print(p,end = " ")
    print(g)

Submission status: Accepted

Solution code:
while True:
    num = int(input())
    if num == 0:
        break
    
    L = [0]*num
    F = 0
    
    cnt = 0
    for i in input():
        L[cnt] += 1
        if i == "M":
            pass
        elif i == "S":
            F += L[cnt]
            L[cnt] = 0
        else:
            L[cnt] += F
            F = 0
        cnt += 1
        cnt %= num
    
    print(*sorted(L), F)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
The Outcome of Bonze
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0280

"""
import sys


def solve(N, C):
    points = [0] * (N+1)
    for i, c in enumerate(C, start=1):
        pos = N if i%N== 0 else i%N
        if c == 'M':
            points[pos] += 1
        elif c == 'S':
            points[0] += (points[pos] + 1)
            points[pos] = 0
        else:
            points[pos] += (points[0] + 1)
            points[0] = 0
    return sorted(points[1:]) + [points[0]]


def main(args):
    while True:
        N = int(input())
        if N == 0:
            break
        C = input()
        ans = solve(N, C)
        print(*ans, sep=' ')


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# from sys import exit
# A, B, C = [int(n) for n in input().split()]
while(True):
    N = int(input())
    if N == 0:
        break
    S = str(input())
    L = len(S)
    F = {"M":0, "S":0, "L":0}
    hands = [{"M":0, "S":0, "L":0} for _ in range(N)]

    cur = 0
    while(cur != L):
        if S[cur] == "M":
            hands[cur%N]["M"] += 1
        elif S[cur] == "S":
            F["M"] += hands[cur%N]["M"]
            hands[cur%N]["M"] = 0
            F["S"] += hands[cur%N]["S"] + 1
            hands[cur%N]["S"] = 0
            F["L"] += hands[cur%N]["L"]
            hands[cur%N]["L"] = 0
        else:
            hands[cur%N]["M"] += F["M"]
            F["M"] = 0
            hands[cur%N]["S"] += F["S"]
            F["S"] = 0
            hands[cur%N]["L"] += F["L"] + 1
            F["L"] = 0
        cur += 1

    ans = [sum(d.values()) for d in hands]
    ans = sorted(ans)
    for e in ans:
        print(e, end=" ")
    print(sum(F.values()))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

while True:
    N = int(input())
    if N == 0:
        break
    
    seq = input()
    cards = []
    for i in range(N):
        cards.append(0)
    yama = 0
    
    for i in range(100):
        id = i % N
        if seq[i] == "M":
            cards[id] = cards[id] + 1
        elif seq[i] == "S":
            yama = yama + cards[id] + 1
            cards[id] = 0
        elif seq[i] == "L":
            cards[id] = cards[id] + yama + 1
            yama = 0

    cards.sort()
    for i in range(N):
        print(cards[i], end=" ")
    print(yama)

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:
        break

    data = list(input())

    ba = 0
    hand = [0] * n
    for i in range(100):
        turn = i % n
        card = data[i]
        if card == "M":
            hand[turn] += 1
        elif card == "S":
            ba += hand[turn] + 1
            hand[turn] = 0
        else:
            hand[turn] += ba + 1
            ba = 0

    hand.sort()
    hand.append(ba)
    print(' '.join(str(h) for h in hand))

Submission status: Runtime Error

Solution code:
while True:
    N = int(input())
    if N==0:break
    dataset = input()
    mens = ["" for _ in range(N)]
    i = 0
    p = ""
    while dataset:
        if i == N: i = 0
        card, dataset = dataset[0], dataset[1:]
        if card == "M":
            mens[i] += card
        elif card == "S":
            p += card + mens[i]
            mens[i] = ""
        else:
            mens[i] += card + p
            p = ""
        i += 1
    print(*[len(s) for s in sorted(mens)], len(p))

Submission status: Runtime Error

Solution code:
while True:
    N = int(input())
    if N==0:break
    dataset = input()
    mens = ["" for _ in range(N)]
    i = 0
    p = ""
    while dataset:
        if i == N: i = 0
        card, dataset = dataset[0], dataset[1:]
        if card == "M":
            mens[i] += card
        elif card == "S":
            p += card + mens[i]
            mens[i] = ""
        else:
            mens[i] += card + p
            p = ""
        i += 1
    print(*[v for v in sorted(len(m) for m in mens)], len(p))