We need to parse this problem statement carefully. It appears to be about a card game called "Buddhist turning"? Actually "Hyakunin Isshu" is a Japanese poetry set. There is a game known as "karuta". But here they describe a game using three types of cards: "men" (maybe '面'? Actually could be 'face'?), "boys" (shaven) and "princesses". Actually the terms: "men", "shaven", "princesses". But in the description: "Use a total of 100 cards, including 64 "men", 15 "boys", and 21 "princesses"." Then they refer to "shaven" (maybe it's "shaven" meaning bald? Actually "shaven" could be a mis-translation; perhaps "boy" is 'shonen' and "shaven" is something else? Wait: The problem says: "If the drawn card is a man, the person who draws will get the card. If the drawn card is a shaven, the person who draws puts all the cards he has, including that card, into the 'field'. If the drawn card is a princess, the person who draws gets all the cards in play, including that card." So there are three types: Man (M), Shaven (S), and Princess (L? Actually example uses M, S, L. So M for man, S for shaven, L for princess).
We need to simulate the game: There are N participants. We have a deck (the "bill pile") of 100 cards, with known order (given as a string of length 100 consisting of M, S, L). The cards are drawn one by one, with participants taking turns in cyclic order starting from participant 1? The rules: "Draw one card from the first participant in order. After the Nth person, repeat from the first person." So the turn order is cyclic: participant 1 draws first, then 2, ..., N, then back to 1, etc. The deck is turned over and mixed, but we are given the exact order of the 100 cards as they will be drawn (i.e., c1 is the top card drawn first, c2 next, etc). So we can simulate.
Each participant initially has 0 cards. There is a "field" which initially is empty? The problem mentions "field" when shaven or princess: "puts all the cards he has, including that card, into the 'field'." Also "gets all the cards in play, including that card." The "field" likely holds cards that have been placed there. For princess, the person who draws gets all the cards in play, including that card. "All the cards in play" probably means all cards currently in the field (the playing area) plus the drawn card? Or "in play" might refer to the field? Typically in such games, there is a central pile (field) and players have personal piles. The rules:
-
If M: the player adds that card to his own hand/pile (i.e., his collection). So he gains 1 card.
-
If S ("shaven"): the player puts all the cards he has, including that card, into the field. That means the player loses all his own cards (they go to the field), and also the S card itself is placed in the field (since it's included). So essentially, the player's total becomes 0, and the field gains the player's previous cards plus the S card.
-
If L ("princess"): the player gets all the cards in play, including that card. "In play" likely refers to the field. So the player takes all cards currently in the field (if any) and also gets the L card (so adds to his hand). Then the field becomes empty. Note: The princess card might be considered added to his hand directly, or also initially placed? But rule says "gets all the cards in play, including that card". So he takes the field and also gets that card. So effectively, the player gains (field size + 1) cards, and field becomes 0.
But careful: In the case of L, does the L card itself go to his hand? Probably yes. But the phrasing: "including that card" suggests that the drawn princess card is part of what he gets, along with all other cards that are "in play". Possibly "in play" might also include his own hand? But usually "in play" means the field (common area). Also typical karuta game "Buddhist turning"? Actually there is a game called "Daihinmin"? Not sure. Let's think of the typical game: There is a "bomb" or something. Actually the description matches a known simple game: "Daifugō"? Not exactly. But we can deduce from the examples.
Let's analyze the given examples.
Example 1:
Input:
2
SSSSSSSSSSSSSSSLLLLLLLLLLLLLLLLLLLLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
That's a string of 100 characters. Count: Let's count: S repeated? "SSSSSSSSSSSSSSS" that is 15 S? Actually "SSSSSSSSSSSSSSS" is 15 S? Let's count: it says: "SSSSSSSSSSSSSSS" that is 15 S's (since 15 S's makes sense: there are 15 "boys" as per card counts: 15 shaven). Then "LLLLLLLLLLLLLLLLLLLLL" That appears to be 21 L's? Let's count: "LLLLLLLLLLLLLLLLLLLLL" = 21 L's? Possibly. Then "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM" that is 64 M's? So indeed the deck order: first 15 S, then 21 L, then 64 M. That matches counts: 15 S, 21 L, 64 M. So this deck is exactly all S's, then all L's, then all M's.
With N=2 participants.
Output: "42 58 0". So after the game, we output the number of cards each participant has in ascending order, then the number of cards remaining in the field.
Here: two participants, so we output two numbers (their final counts) sorted ascending, then field count. So output "42 58 0". So one player ended with 42, the other with 58, and field 0.
Now, we need to simulate and see if rules produce that.
Let's simulate.
N=2, participants 0 and 1 (or 1 and 2). We'll denote turn order: player 0 draws first, then player 1, then player 0, etc., until 100 cards drawn.
Deck: 15 S, then 21 L, then 64 M.
Let's simulate step by step.
Initialize: p0 = 0, p1 = 0, field = 0.
Card #1 (first): S, drawn by p0.
Rule for S: player puts all the cards he has, including that card, into field. p0 has 0, so he puts the S card into field. So after: p0 = 0, field = 1 (the S card). p1 = 0.
Card #2: second card: S, drawn by p1.
p1 has 0, so puts that S into field: p1 = 0, field = 2.
Card #3: third card: S, drawn by p0. p0 has 0, field becomes 3.
Continue: all 15 S cards are drawn. Since both players have 0 each time, each S just adds to field. After 15 S, the field = 15, both players 0.
Who drew which? With 2 players, draws alternate: p0 gets odd-numbered cards (1,3,5,...,15? Actually card indices 1-based: 1 p0, 2 p1, 3 p0, 4 p1, ...). Since 15 is odd, the last S (15th) is drawn by p0 (since odd). So after 15 S, field = 15, p0=0, p1=0.
Now next: 21 L cards.
Card #16: first L, drawn by p1 (since after p0's 15th draw, next is p1? Let's track carefully: after 15 cards, we have drawn: cards 1-15. The next card is #16, drawn by next player. Turn sequence: after p0 draws card 1, turn goes to p1 for card2, then p0 for card3, etc. So parity: card number i: if i is odd -> p0; even -> p1. So for i=16 (even) -> p1. So p1 draws L.
Rule for L: player gets all the cards in play (field) including that card. So p1 takes field (15 cards) plus the L card itself, so p1 gains 15+1 = 16 cards. Field becomes 0. So after card16: p0=0, p1=16, field=0.
Card #17: L, drawn by p0 (odd). Now field=0. For L: p0 gets field (0) + the L card = 1. So p0 gains 1. So p0=1, p1=16, field=0.
Card #18: L, drawn by p1. p1 gets field(0)+1 =1 => p1=17, field=0.
Card #19: L, drawn by p0 => p0=2.
... This pattern: each L gives the drawing player +1, because field is empty each time (since after each L, field becomes 0, but wait, does the L card go to field initially? The rule says "gets all the cards in play, including that card." The card is not placed into field first; the player just takes it directly. So after an L, field is 0 (since before the L, field may have been something, but the player takes it all). So after the first L, field became 0. Subsequent L's happen with field=0, so each L just gives +1 to the drawer.
So for the remaining L cards, we have 20 left (since total L=21, one used). They will be drawn alternating: p0 gets odd-numbered L among the remaining, p1 gets even. Let's count: after card16 (p1), we have L at positions 17-36? Actually there are 21 L total, so from 16 to 36 inclusive? Actually card index: first L was #16, last L #36 (since 15 S + 21 L = 36). So #16 (p1), #17 (p0), #18 (p1), #19 (p0), #20 (p1), #21 (p0), #22 (p1), #23 (p0), #24 (p1), #25 (p0), #26 (p1), #27 (p0), #28 (p1), #29 (p0), #30 (p1), #31 (p0), #32 (p1), #33 (p0), #34 (p1), #35 (p0), #36 (p1). Let's count: from 16 to 36 inclusive is 21 numbers. Among these, even indices: 16,18,20,22,24,26,28,30,32,34,36 => 11 even indices? Actually 16 to 36: list evens: 16,18,20,22,24,26,28,30,32,34,36 = 11 evens. Odds: 17,19,21,23,25,27,29,31,33,35 = 10 odds. So p1 (even) gets 11 L cards (including #16) and p0 gets 10 L cards. But note that #16 gave p1 also field=15, so that's not just +1. So after all L cards, let's compute totals.
We had after #16: p0=0, p1=16.
Now for each subsequent L:
For each p0 L: p0 += 1.
For each p1 L: p1 += 1.
Number of remaining L for p0: from #17 to #35 odd -> 10 L's, so p0 gains 10 => p0 = 10.
Number of remaining L for p1: from #18 to #36 even -> 11 L's, so p1 gains 11 => p1 = 16 + 11 = 27? Wait after #16 p1=16, plus 11 = 27. But also need to check #36 is even, p1 draws it. So p1 total from L after all: p1 = 16 (from first L) + 11 (from the other 11 L's) = 27. p0 = 10.
So after L cards (cards 1-36), we have p0=10, p1=27, field=0.
Now M cards: 64 M, from card #37 to #100. For M: the drawer gets the card (+1). No effect on field.
Turns: after card #36, turn was p1 drew. So next card #37: p0 (since alternate). #37: p0 gets +1 => p0=11.
#38: p1 +1 => p1=28.
#39: p0 => 12
#40: p1 => 29
... So M cards are 64 cards. Since players alternate, each will get half? 64 even, so each gets 32 if start with p0. Let's confirm: after #36, p1 drew last, so #37 p0, #38 p1, #39 p0,... So pattern: p0 gets odd-numbered M cards among the 64, p1 gets even. Count: total M=64. Starting at #37 (p0) and ending at #100? #100 =? Let's compute: 36+64=100, so last M is #100. Indices 37-100 inclusive: count=64. Among these, odds: 37,39,...,99? Let's see: from 37 to 100, number of odds = (100-37)/2+1 if both inclusive? Actually number of integers from a to b inclusive is b-a+1. Count of odds: if start odd, then pattern odd/even alternating. Since 37 is odd, and total count 64 is even, exactly half odd, half even. So p0 gets 32 M, p1 gets 32 M.
Thus after all M, p0 = 10 + 32 = 42, p1 = 27 + 32 = 59. Wait p1 = 27+32=59, but expected output says 42 and 58. So p1 should be 58, not 59. Something off.
Maybe field after all is 0, as output shows 0. So our p1 is 59 vs expected 58.
Let's re-check L counts: The problem states there are 15 S, 21 L, 64 M. In the input string: "SSSSSSSSSSSSSSSLLLLLLLLLLLLLLLLLLLLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM". That's 15 S, 21 L, 64 M. In our simulation, after S: field=15. Then first L: p1 takes field (15) and the card itself -> p1 gets 16. Then remaining 20 L? Actually total L=21, so after first, there are 20 left. Our earlier count gave p0 10 L, p1 11 L? But wait, we said p1 gets 11 total L including first? Actually first L counted separately. So we counted p1's other L as 11, but that would mean total L p1 got = first L (1) + 11 others = 12 L? But we previously said p1 got 11 even indices from 16 to 36 inclusive, but even indices: 16,18,20,22,24,26,28,30,32,34,36 = 11 numbers. That's the total L that p1 draws (including the first one at 16). So p1 draws 11 L cards total. p0 draws 10 L cards total. So that matches: 11+10 = 21. So p1 drew 11 L cards, p0 drew 10 L cards.
Now effect of each L: for the first L (by p1), p1 gets field (15) + that card (1) = 16, so net gain of 16 from that draw. For subsequent L draws, since field is 0, each gives net gain of +1. So p1's total from L = 16 + (11-1)*1 = 16+10 = 26? Actually if p1 draws 11 L cards, and one of them (the first) gave +16, and the other 10 gave +1 each, so total gain from L for p1 = 16 + 10 = 26. But we earlier said p1 got 16 from first L and then 11 more from the rest, that's 16+11 = 27? Actually we thought there were 11 others, but it's 10 others. Let's recount: Total L draws by p1 = 11. Among them, the first one gave +16, and the other 10 gave +1 each => total 16+10 = 26. p0 draws 10 L, each gives +1, total from L = 10. So after L, p0 = 10, p1 = 26. Then M: each get 32. So final p0 = 10+32=42, p1=26+32=58. That matches expected output 42 and 58! So indeed we mis-counted: we thought p1 had 11 others, but it's actually 10 others. Let's double-check the count of L indices.
Indices from 16 to 36 inclusive: total 21 numbers.
List them with drawer:
16: p1 (even)
17: p0
18: p1
19: p0
20: p1
21: p0
22: p1
23: p0
24: p1
25: p0
26: p1
27: p0
28: p1
29: p0
30: p1
31: p0
32: p1
33: p0
34: p1
35: p0
36: p1
Count p1: indices: 16,18,20,22,24,26,28,30,32,34,36 -> that's 11 indices. So p1 draws 11 L cards. Among these, one is the first (16) and the rest are 10 others? Actually 11 total, so besides the first there are 10 more. Yes, correct. So p1's extra L draws = 10. So p1 gets +16 from first, +10 from rest = 26. p0 draws 10 L = +10. So after L: p0=10, p1=26.
Thus final = 42,58,0. Good.
So the rules as described are correct: For S, the player puts all his cards (including S) into field, meaning he loses his own cards and adds them plus the S card to field. For L, the player takes all cards from the field (if any) and also takes the L card (so gains field+1), and field becomes 0.
Now, we need to simulate for any given order.
The problem says: "Given the number of participants and the order of the cards piled up in the bill pile, the number of cards each participant has at the end of the game is arranged in ascending order, and the number of cards remaining in the field is shown." So for each dataset, we output the final counts for each participant (sorted ascending) and then the field count.
Constraints: N from 2 to 10, datasets up to 100, deck fixed length 100.
We need to simulate the game precisely.
Potential tricky points: The turn order is cyclic starting from participant 1. That is standard. We'll index participants from 0 to N-1 for programming. Current player index starts at 0 (first participant). After each draw, we increment player index modulo N.
What about the interactions when an S is drawn: The player puts all the cards he has, including that card, into the field. That means: add the player's current total to the field, then set player's total to 0. Also, the S card itself is considered part of what he has? Actually he puts all the cards he has, including that card, into the field. At the moment of drawing, he gets the card, but then he puts it along with his existing cards into field. So effectively: field += player + 1 (since the card is included), player = 0. That seems correct.
But what about the timing: Does the player first get the card (like M) and then put it? The rule states: "If the drawn card is a shaven, the person who draws puts all the cards he has, including that card, into the 'field'." That implies that the S card is included among the cards moved to the field. So the player effectively loses his entire collection and also the S card goes to field, not to his hand. So net: player's count becomes 0, field gains (player's previous count + 1).
Now for L: "If the drawn card is a princess, the person who draws gets all the cards in play, including that card." So the player gains all cards currently in the field, plus the L card. So net: player += field + 1; field = 0.
What about the scenario when both happen in same draw? Not possible, since one card is one type.
Edge cases: When field is empty and L is drawn: player gains 1 (the L card). That's consistent.
Also, when S is drawn by a player who currently has 0: field gains 1 (the S card). Player stays 0.
Now, we need to handle the ordering of final counts: output the number of cards each participant has, sorted in ascending order (non-decreasing), then the field count. So if there are ties, just sort.
Check second example:
Input:
2
SSSSSSSSSSSSSSLLLLLLLLLLLLLLLLLLLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMSL
Let's parse this string: It seems they've given a string of 100 characters. Count: "SSSSSSSSSSSSSS" that is 14 S? Wait: "SSSSSSSSSSSSSS" is 14 S? Actually they wrote "SSSSSSSSSSSSSSLLLLLLLLLLLLLLLLLLLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMSL". Let's count: There are 15 S's in total originally, but they wrote 14? The string: "SSSSSSSSSSSSSS" (14) then "LLLLLLLLLLLLLLLLLLLL" (maybe 20 L?) then "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM" (maybe 64 M) then "S" then "L"? Actually the end "SL" suggests two extra cards? But total should be 100. Possibly the distribution is different: maybe the input has 15 S, 21 L, 64 M but in different order. Let's count characters:
"SSSSSSSSSSSSSS" is 14 S (since 14 S's). Then "LLLLLLLLLLLLLLLLLLLL" that is 20 L? (since 20 L's). Then "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM" that is 64 M? Actually "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM" seems to be 64 M? But then "SL" at the end makes total 14+20+64+2 = 100, but then we have 14+2 = 16 S? Actually there is an S at end, so total S becomes 15? Let's count properly: The string:
"SSSSSSSSSSSSSS" (14 S)
"LLLLLLLLLLLLLLLLLLLL" (20 L)
"MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM" (64 M)
"S" (1 S)
"L" (1 L)
So total: 14+20+64+1+1 = 100. So S count = 15, L count = 21, M count = 64. Yes matches the composition. So deck order: 14 S, then 20 L, then 64 M, then an S, then an L. So S's are not all at start; one S is near the end, and one L is last.
Now output: "0 100 0". So one player ends with 100, the other 0, field 0.
Let's simulate to verify.
N=2, players 0 and 1. Deck order given as above.
We'll simulate quickly.
Initialize: p0=0, p1=0, field=0. Turn: p0 draws first.
We need to go through 100 cards.
Deck: first 14 S. Let's process:
Card1 (S) p0: p0=0 -> field += 1, p0=0 -> field=1.
Card2 (S) p1: field=2.
Card3 (S) p0: field=3.
Card4 (S) p1: field=4.
Card5 (S) p0: field=5.
Card6 (S) p1: field=6.
Card7 (S) p0: field=7.
Card8 (S) p1: field=8.
Card9 (S) p0: field=9.
Card10 (S) p1: field=10.
Card11 (S) p0: field=11.
Card12 (S) p1: field=12.
Card13 (S) p0: field=13.
Card14 (S) p1: field=14.
After 14 S, p0=0, p1=0, field=14.
Next: 20 L cards (cards 15-34). Let's index: card15 = L, drawn by p0 (since after p1 drew card14, next is p0). So card15: p0 draws L. field=14. For L: p0 gets field + 1 = 15. So p0=15, field=0.
Card16: L, drawn by p1: field=0 -> p1 gets 1 -> p1=1.
Card17: L, p0: p0=16.
Card18: L, p1: p1=2.
... Continue alternating for 20 L total. Let's count how many L each gets.
Total L = 20? Actually we have 20 L in this segment (cards 15-34). But note later there is an additional L at the very end (card100) so total L = 21. So after the first 14 S, we have 20 L, then 64 M, then S, then L. So for now, we have 20 L.
We need to process the 20 L in order.
After card15: p0=15, field=0.
Remaining L: cards 16 to 34 inclusive? Let's count: from card15 (L) to card34 (L) might be 20 L. Actually if card15 is the first L, and there are 20 L, then last L in this segment is card34. But we need to verify: The string: after 14 S, then 20 L. So positions: 1-14: S; 15-34: L (20 cards); 35-98: M? Actually 64 M from card35 to card98? Because 14+20=34, so M from 35 to 98? 34+64=98, yes. Then card99: S; card100: L.
So card15-34: 20 L.
Now, after card15 (p0 L), we have 19 L left. Who draws which? Starting with card15 p0, then card16 p1, card17 p0, ... alternating.
Let's compute number of L draws for each in this segment.
Total L in segment = 20. Starting player for first L is p0. So p0 draws L at odd offsets within the segment: card15 (1st), card17 (3rd), ... up to the 19th? Since 20 L, indices: 1st to 20th. p0 gets those with odd positions: 1,3,5,...,19 -> that's 10 L (since (19-1)/2+1 =10). p1 gets even positions: 2,4,...,20 -> also 10 L.
So after these 20 L, p0 will have: initial from first L: 15 (which included the field) plus gains from the other 9 L draws (since p0 gets total 10 L, including the first). So p0 gets 1 additional for each of the remaining 9 L draws (since field is 0 after first L). So p0 total from this segment = 15 + 9 = 24? But careful: p0's first L gave +15. Then each subsequent L for p0 gives +1. So p0 total from L segment = 15 + (10-1)*1 = 15+9 = 24. p1 gets 10 L draws, each giving +1 (since field remains 0 after the first L? Actually after card15, field=0, and subsequent L never cause field to become non-zero because L only takes from field, but if field=0, then they just gain 1. However, note: if a player draws L when field is not zero, they take all. But after the first L, field=0. Could there be a situation where field becomes non-zero again before later L? Only S or maybe something else? In this segment there are only L, no S. So field remains 0 after the first L. So p1's 10 L each give +1: p1 total from L segment = 10.
But p1 also had something before? p1 was 0 before L segment. So after L segment, p0=24, p1=10, field=0.
Now M cards: cards 35 to 98 = 64 M. After L segment, last drawn card was card34? Actually card34 is last L, drawn by? Since total L=20, with even number, the last L (20th) is drawn by p1 because even positions: 20th is even, so p1. So after card34, next player is p0 for card35.
Now process 64 M: p0 and p1 alternate, starting with p0 for card35. 64 is even, each gets 32. So p0 gains 32 -> p0=24+32=56; p1 gains 32 -> p1=10+32=42.
Now card99: S, drawn by? After card98 (the last M), who drew that? Let's determine: M cards from 35 to 98 inclusive: 64 cards. Starting with p0 at 35, so 35 p0, 36 p1, 37 p0, ... The pattern: p0 draws odd-indexed among these (relative to segment start). Since 64 even, the last M (card98) will be drawn by? If we start with p0 at 35 (which corresponds to an odd overall index? Actually overall index 35 is odd? 35 is odd, so p0. So parity: odd overall index -> p0; even -> p1. So card98 index is even? 98 is even, so p1 draws card98. So after card98, next player is p0 for card99.
Card99: S, drawn by p0.
Current: p0=56, p1=42, field=0.
S rule: p0 puts all his cards (including the S) into field. So field becomes 56+1 =57? Actually p0's cards = 56, plus the S card adds 1, so field = 56 + 1 = 57. p0 becomes 0.
Now card100: L, drawn by p1 (since after p0, next is p1).
L rule: p1 gets all cards in field (57) plus the L card itself. So p1 gains 57+1 = 58. Field becomes 0. So p1 becomes 42 + 58 = 100. p0 remains 0.
Thus final: p0=0, p1=100, field=0. Sorted ascending: 0, 100, 0 output -> "0 100 0". Matches example.
Good.
Third example:
Input:
5
MMMMMMSLSLLMMMSMMSLMMMLMMMMLSLLLLMLSMMLMMLLMSSSLMMMMLMLSMLMSMMMMMMMSMMMMMMLMMMMMSMMMLMMLMMMMMMMMMSSM
Output:
0 0 3 10 59 28
So N=5 participants. Output: 5 numbers for participants sorted, plus field: so 6 numbers: 0 0 3 10 59 28. So participants counts: 0,0,3,10,59; field=28.
We can simulate to verify later but seems plausible.
Thus the simulation is correct.
Now we need to implement.
We have to handle up to 100 datasets, N up to 10. Simulation is trivial: O(100) per dataset.
Implementation steps:
-
Read lines until a line "0".
-
For each dataset, read N, then read the next line (a string of 100 characters). Possibly there could be whitespace? The input says: "c1c2 ... c100" with no spaces, so it's a single string of length 100. Possibly there might be newline issues. We'll just read it as a string. Might be that there are spaces? The sample shows no spaces. So we can use input().strip().
-
Initialize:
players = [0] * N
field = 0
current_player = 0 (index 0 for first participant)
-
For each character c in the card string (length 100):
if c == 'M':
players[current_player] += 1
elif c == 'S':
# player puts all his cards + this card into field
field += players[current_player] + 1
players[current_player] = 0
elif c == 'L':
# player takes all field + this card
players[current_player] += field + 1
field = 0
else: # maybe other? But should not.
pass
advance to next player
current_player = (current_player + 1) % N
-
After processing all 100 cards, we have final players list and field.
-
We need to output: sorted list of players (ascending) and then field.
-
Print them separated by spaces. Ensure no trailing space.
Implementation considerations: The input may contain blank lines? Possibly not. But we need to handle multiple datasets. The termination line is a single "0". Could be that the line containing 0 might have spaces? Usually it's just "0". We'll read as integer, if 0 then break.
Edge Cases: What if N is given but then the next line might be empty? Unlikely.
Testing: Should work with examples.
But wait: There is subtlety: When S is drawn, "puts all the cards he has, including that card, into the field." Does that mean the S card is added to the field, but does the player also lose his own cards? Yes, he puts all the cards he has, including that card. So the player's own cards are moved to the field. So the net is as we coded.
When L is drawn, "gets all the cards in play, including that card." The "in play" likely means the field. Does it also include any cards the player has? Probably not. So we add field + 1 to player, and set field=0.
One more nuance: Could there be a scenario where the player draws an L and there are cards in the field; he takes them; that's fine.
Now, about sorting: "arranged in ascending order". So we sort the list of players (size N) in ascending order. Then append field.
Print each number separated by one blank. So we can convert to strings and join.
Now, confirm with third example quickly to ensure we are consistent. Let's simulate third example with N=5 and given deck. But it's messy; we can test later.
Potential pitfalls: The string length may be exactly 100, but we should not assume; we can trust input.
Now, about participants indexing: The problem says "first participant in order", so we start with participant 1 (which we can call 0-index). The order is 1,2,...,N,1,2,... So our simple modulo works.
Now, about the output format: The sample output for third example: "0 0 3 10 59 28". That's 6 numbers. So they printed 5 player counts sorted plus field.
Now, note: The field is included as last number.
So that's it.
But the problem statement also mentions "the number of cards remaining in the field is shown." So we output that.
Now, is there any other rule? Not from description.
We should consider if there is any rule about when the bill pile is empty, the game ends, and we output results. So we don't need to consider any extra steps.
Now, before coding, check if there is any tricky interplay: When S is drawn, the player puts all his cards into field, including the drawn card. That means his total becomes 0, and field gains his previous total + 1. However, if the player had 0, field gains 1. That's fine.
When L is drawn, the player gets field + 1, and field becomes 0.
One question: Could there be any effect of the order of operations? For instance, if the player draws L, does he first add the card to his hand, then take field? Or take field including the card? But net effect same.
Now, we need to ensure that the card counts sum to 100 at all times. Initially total cards = 100 (all in deck). As we draw, cards are distributed among players and field. At the end, the sum of players' cards + field should be 100. For first example: 42+58+0=100. Second: 0+100+0=100. Third: 0+0+3+10+59+28 = 100? 0+0+3+10+59+28 = 100. Yes.
So we can sanity-check.
Now implement.
But there is a potential nuance: The input string may have newline characters or spaces; we strip.
Also note: The number of datasets is up to 100, but we need to read until 0.
Implementation in Python:
def solve():
import sys
lines = sys.stdin.read().splitlines()
idx = 0
out_lines = []
while idx < len(lines):
line = lines[idx].strip()
if not line:
idx += 1
continue
N = int(line)
if N == 0:
break
idx += 1
# read next line
if idx >= len(lines):
break # maybe incomplete
cards = lines[idx].strip()
# maybe cards length may be 100; could have spaces? Remove spaces?
# Ensure we have exactly 100 characters by removing any whitespace? But input has no spaces.
# For safety, we could filter only M,S,L.
# But example has contiguous string.
# However, some input lines might have spaces? The problem says no spaces.
# So we'll just take the string as is.
# If length less than 100 (maybe due to line breaks?), but not expected.
# Proceed.
players = [0]*N
field = 0
cur = 0 # index 0..N-1
for ch in cards:
if ch == 'M':
players[cur] += 1
elif ch == 'S':
field += players[cur] + 1
players[cur] = 0
elif ch == 'L':
players[cur] += field + 1
field = 0
# else ignore (should not happen)
cur = (cur + 1) % N
# sort players
sorted_players = sorted(players)
result = ' '.join(str(x) for x in sorted_players) + ' ' + str(field)
# But careful: The output expects numbers separated by one blank; we have an extra space before field? Actually we appended space and field. That's fine.
# However, maybe we should construct list and join.
out_lines.append(result)
idx += 1
sys.stdout.write('\n'.join(out_lines))
But note: The field is also included; we included as separate number after sorted players. But in the output example for N=5, they printed "0 0 3 10 59 28". That is six numbers. Our result string would be "0 0 3 10 59 28". So okay.
We must ensure no trailing spaces. Our join creates proper spaces between numbers and then field. However, if we do ' '.join(sorted_players_str) + ' ' + str(field), that adds a space before field even if players list empty? But players list is never empty (N>=2). So fine.
Alternatively, we can do:
out_list = [str(x) for x in sorted_players] + [str(field)]
result = ' '.join(out_list)
That's cleaner.
Now, one more nuance: Could there be a dataset where the deck string is broken across lines? The problem says "The next line is given a character string that represents the arrangement of the bills piled up in the bill pile." So it's a single line.
So our reading logic using splitlines is fine.
Testing with given examples should produce correct output.
But we should double-check the third example to be absolutely sure.
We can simulate third example manually? Might be long but we can trust.
But we should verify that the simulation algorithm is correct for all cases, especially when S and L interact with field. There's no other hidden rule like if field is empty when L is drawn, you only get the card? Yes we did that.
Now, confirm about S: "puts all the cards he has, including that card, into the field." The phrase "including that card" suggests the S card itself is also put into field. So we added player's cards and +1. However, could it be that the S card is first added to the player's hand (like M) and then all his cards (which now include that S) are moved to field? That yields the same outcome: player's count becomes 0, field gets previous count + 1. So no difference.
Now, about L: "gets all the cards in play, including that card." Could it be that the L card is first placed in the field? That would be weird because if it's placed in field, then the player takes all from field (including that L) -> same outcome: player gets field+1. But if the field already had some cards, then the L would be added first then taken, but that effectively adds 1 to field before taking, so player would get (field_original + 1) and field becomes 0. That is exactly our effect. However, if the card is first added to player's hand, then the player takes field? Then he would get field + (the card in hand?) Actually if he first adds L to hand (+1), then takes field? The rule says "gets all the cards in play, including that card." So it seems the card is part of what he gets; it could be interpreted as he takes the field and also gets the L card as a bonus, which is the same net: +field +1.
So our simulation is consistent.
Thus, the solution is straightforward.
Now, let's consider if there is any other rule: "Buddhist turning" might have special additional rule like when a princess is drawn, you also get something else? But from examples it's fine.
Now, there might be a twist: The deck composition is fixed: 64 M, 15 S, 21 L, but the input gives the order. So we assume it matches that composition. The problem may not guarantee that? It says "Use a total of 100 cards, including 64 "men", 15 "boys", and 21 "princesses"." So the deck has those numbers, but the input string should reflect that composition. Possibly they could give any string of length 100 consisting only of M,S,L, but we can assume it's valid.
Now, note: The input might have extra whitespace like spaces between characters? The problem says "There is no space between each character." So we can ignore.
Now, we should also consider that the field might accumulate cards from S and later be taken by L. That's covered.
Now, about sorting: "arranged in ascending order". So we sort numerically. That's simple.
Now, finalize.
Edge Cases: N=10, and all cards are M, then each player gets 10 cards each? Actually 100 M, N=10, each gets 10 cards if evenly distributed? With turn order starting from player0, after 100 draws, each player gets 10 (since 100/10=10). So output would be "10 10 10 ... 10" (10 times) and field 0. Sorted ascending just all 10s.
If all S, then each S drawn, the player moves his cards (if any) plus the S to field. With all S, starting with all players at 0, each S adds 1 to field and player stays 0. So after all S, field = 100, all players 0. Then output: "0 0 ... 0 100". That seems plausible.
If all L, then first L drawn: field=0 -> player gets 1. Then each subsequent L: field remains 0 (since L takes field and adds 1, field becomes 0). So each L gives +1 to drawer. So final distribution depends on turn order; sum of all players = 100. Then field=0.
All fine.
Now, double-check the third example quickly via code mental? Probably unnecessary.
Thus, solution is done.
But before finalizing, we might want to consider if there is any nuance about the "shaved turn"? The problem says: "There are various derivative types of rules, but the shaved turn considered here is performed by N participants according to the following rules." So it's the "shaved turn" variant. The description we used matches. So okay.
One more nuance: Could it be that when a shaven (S) is drawn, the player puts all the cards he has (excluding the drawn card?) but including that card? Already included.
One more nuance: Could it be that when a princess (L) is drawn, the player takes all cards in play, but not including that card? The phrase "including that card" clarifies that card is included. So our approach is correct.
Thus, answer.
Now, we should ensure we handle reading properly. Since input may contain multiple datasets, and the last line is a single '0', we can read using sys.stdin.
I'll write final code.
But I see a potential problem: The input format says each dataset has two rows. But sometimes there may be blank lines. We can robustly skip empty lines.
Implementation in Python:
import sys
def main():
out = []
while True:
line = sys.stdin.readline()
if not line:
break
line = line.strip()
if line == "":
continue
N = int(line)
if N == 0:
break
# read next line; may need to skip empty lines
cards_line = sys.stdin.readline().strip()
while cards_line == "":
cards_line = sys.stdin.readline().strip()
# cards_line should be 100 chars; but we can just iterate.
players = [0]*N
field = 0
cur = 0
for ch in cards_line:
if ch == 'M':
players[cur] += 1
elif ch == 'S':
field += players[cur] + 1
players[cur] = 0
elif ch == 'L':
players[cur] += field + 1
field = 0
# else ignore? But shouldn't happen.
cur = (cur + 1) % N
sorted_players = sorted(players)
out.append(' '.join(str(x) for x in sorted_players + [field]))
sys.stdout.write('\n'.join(out))
if name == 'main':
main()
But careful: sorted_players + [field] is concatenating list of ints? Actually sorted_players is list of ints, [field] is list of int, so we can do sorted_players + [field] then map to str. So:
result = sorted_players + [field]
out.append(' '.join(str(x) for x in result))
Yes.
Now, all good.
We should test with the given examples.
Let's manually run the first example with our code:
N=2, cards = "SSSSSSSSSSSSSSSLLLLLLLLLLLLLLLLLLLLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
After simulation, we got players = [42,58] (depending on indexing order, but sorted yields [42,58]), field=0 -> output "42 58 0". Yes.
Second: "0 100 0".
Third: We'll trust.
Thus, final answer.
Now, we may also consider the scenario where there is a trailing newline after the last dataset? Our code handles break on N==0.
So final solution is straightforward.
But the problem might be from a contest, maybe they expect to handle multiple datasets quickly. Python is fine.
Now, let's double-check the third example using quick simulation to ensure no off-by-one.
We'll simulate third example with N=5 and the given card string.
Given:
cards = "MMMMMMSLSLLMMMSMMSLMMMLMMMMLSLLLLMLSMMLMMLLMSSSLMMMMLMLSMLMSMMMMMMMSMMMMMMLMMMMMSMMMLMMLMMMMMMMMMSSM"
We need to ensure length is 100. Let's count? We'll trust.
Let's simulate step by step with Python mental? That's tedious, but we can maybe approximate.
But we can write a quick test in our mind? Let's not.
But we can ensure our logic is correct by reasoning about typical behavior.
We'll produce code and trust that the expected output matches.
But since we are solving, it's okay.
Thus, solution is ready.
Now, I'll produce final answer with explanation and code.
But note: The problem says "Create a program to output." It's a programming task. So we just need to provide code.
We also need to ensure we read until a line with "0". The input may have spaces? The line with "0" may have leading/trailing spaces; we strip.
Now, think about potential pitfalls: The string may be exactly 100 characters, but if there are spaces, we can filter out spaces: e.g., cards = ''.join(cards_line.split()) to remove whitespace. Could be safe.
But given the spec, I think it's okay.
Now, let's produce final solution.
One more nuance: Could there be a dataset where the deck string is broken across multiple lines? The problem says one line. So we trust.
Now, final code.
Time complexity: O(100 * number_of_datasets) <= 100*100 = 10000, trivial.
Thus, answer.
Now, I'll write a clear explanation and provide code.
We'll also include comment.
So final answer.
```python
import sys
def solve() -> None:
out_lines = []
while True:
line = sys.stdin.readline()
if not line:
break
line = line.strip()
if line == "":
continue
N = int(line)
if N == 0:
break
# read the card string (may contain whitespace only if input is malformed)
cards_line = sys.stdin.readline().strip()
while cards_line == "": # skip possible empty lines
cards_line = sys.stdin.readline().strip()
# keep only M, S, L characters (remove any stray whitespace)
cards = ''.join(ch for ch in cards_line if ch in "MSL")
players = [0] * N
field = 0
cur = 0 # index of the current player, 0‑based
for ch in cards:
if ch == 'M':
players[cur] += 1
elif ch == 'S':
field += players[cur] + 1
players[cur] = 0
elif ch == 'L':
players[cur] += field + 1
field = 0
# advance to next player
cur = (cur + 1) % N
# output: sorted player counts + field
result = sorted(players) + [field]
out_lines.append(' '.join(str(x) for x in result))
sys.stdout.write('\n'.join(out_lines))
if name == "main":
solve()