We need to parse multiple datasets. Each dataset consists of three lines? The description says: "Each dataset is given in the following format:
record1
record2
record3
The i-line is given a string that represents the serve order of the i-game."
Thus each dataset consists of three records, each for one game? But the example input shows:
ABAABBBAABABAAABBAA
AABBBABBABBAAABABABAAB
BABAABAABABABBAAAB
AABABAAABBAABBBABAA
AAAAAAAAAAA
ABBBBBBBBBB
0
So there are 6 records then a 0. That is 6 records, which would be 2 datasets? Because each dataset should have 3 records. But 6/3=2 exactly. So the example input contains two datasets: first three lines (records for games 1,2,3) then next three lines (records for games 1,2,3) then 0. Let's check: Output shows 6 lines, each with two numbers. Indeed there are 6 output lines. So each dataset yields 3 lines of output, each corresponding to the score for that game.
Thus for each dataset, we process three strings, each representing the serve order (i.e., sequence of serves) for a game. From this string we must determine the final score of that game.
We need to understand the rules of the game: It's a badminton singles game to 11, but with specifics: "3 Play the game." Possibly means they play a match of 3 games? Actually the description: "Mr. A and Mr. B became players, and Mr. C became a referee and played a badminton singles game. The rules decided by the three people are as follows.
- 3 Play the game.
- The person who gets 11 points first wins the game.
- The first serve of the first game starts with Mr. A, but the next serve is done by the person who got the previous point.
- In the 2nd and 3rd games, the person who took the previous game makes the first serve.
- After 10-10, the winner will be the one who has a difference of 2 points."
Interpretation: They are playing a match of up to 3 games (i.e., best of 3). But the input only gives the serve order for each game (3 games). So we need to compute the score of each game, given the serve order (who served each point). The serve order includes every point, including those after deuce? And we need to deduce points based on serve rules? Wait: The record is the person who hit the serve (i.e., who served each rally). But we don't know who won the point. In badminton, the serve is hit by one player, and the rally is played. The point winner could be either the server or the receiver. However, the record only shows who served. So how can we deduce the score? We need additional info: Usually, in badminton scoring, you only score points when you win the rally, and the server changes if you lose the rally? Actually modern badminton uses rally point scoring: a point is scored on every rally, by the winner. The next serve is by the winner of the rally. So the serve order (who served each rally) is determined by who won the previous point: the winner serves next. So we have a direct relationship: If we know the sequence of points (who won each rally), we could determine the serve order: start with initial server; then after each point, if winner is server, same server continues? Wait: In badminton, the winner of a rally scores a point and serves next. Actually, let's recall: In rally point scoring used in badminton (since 2006), a point is scored by the winner of each rally. The winner of the rally serves next. So the server for the next rally is always the winner of the current rally. So the sequence of servers is determined by the sequence of point winners: initial server S1; after first rally, if winner = W1, then next server = W1; after second rally, if winner = W2, then next server = W2; and so on.
Thus if we have the sequence of servers (who served each rally), we can deduce who won each point, because the server for the next rally is the winner of the current rally. Let's formalize:
Let serve_sequence[0] = initial server (given: for game1 it's always A; for game2 and game3, it's the winner of previous game? Actually rule: "In the 2nd and 3rd games, the person who took the previous game makes the first serve." So the initial server for game2 is the winner of game1. Similarly for game3, initial server is the winner of game2. But note: we don't know the winner of previous games until we compute scores. However, we are processing each game's serve order independently? Actually we need to know the initial server for each game. For game1, it's A. For game2, it's the winner of game1. To compute the score of game2, we need to know that winner. So the games are not independent; they depend on the outcome of previous games to determine the initial server of the subsequent games. But the input gives us the serve order for each game. So we can use the serve order to infer the points, but we also need to know initial server. For game1 we know it's A. For game2 we need to know who won game1. But we are computing the scores for all three games, presumably in order. So we can compute game1 first, determine winner, then set initial server for game2 accordingly, then compute game2, determine winner, then initial server for game3.
Thus we need to decode the score from the serve sequence. Given serve_sequence S[0..n-1] (n rallies, each rally has a server). The sequence length is the number of rallies (points played) until the game ended. But we don't know n. Actually the record is the serve order for the entire game, i.e., the sequence of who served each rally. So the length of the string equals the number of rallies played in that game. We need to compute the final scores of A and B.
We know the initial server: for game1 it's A; for later games it's the winner of previous game. But we also need to know the rules: The game ends when a player reaches 11 points and is at least 2 points ahead, except after 10-10, they must win by 2. So we need to simulate the game, using the serve sequence to deduce point winners.
Given serve sequence S[0], S[1], ..., S[N-1] (N rallies). We know S[0] is the initial server. For i from 0 to N-2, we can deduce that the winner of rally i is S[i+1] (because next server is the winner). And for the last rally (i = N-1), we cannot deduce the winner from next server because the game ends after that point. But we can infer winner from the final score? Actually we need to compute the score based on all the points. We know that after each point, the score updates. And the game ends when the score satisfies winning condition. So the serve sequence length N is the total number of points played. We need to reconstruct the sequence of point winners: W[0..N-1] where W[i] is the winner of rally i. For i=0..N-2, W[i] = S[i+1]. For i=N-1, we need to determine W[N-1] from the final scores? Actually we need to compute the final scores, which are determined by the count of points won by each player. Let a = number of points won by A, b = number of points won by B. Since each rally produces a point for exactly one player, a+b = N. Also we need to satisfy that the final score is a valid ending: (a>=11 or b>=11) and (if max(a,b)>=11 and difference>=2). Additionally, after the last point, the game ends, so at that moment the scores satisfy winning condition; before the last point, the condition was not met (i.e., no player had reached 11 with sufficient lead, or perhaps it could be that the condition was just met earlier? The game ends as soon as the winning condition is satisfied, so there should be no further points after the condition is met. So the sequence of winners must be such that before each point, the condition is not satisfied; after the final point, it is satisfied.
We also have the initial server known (or deduced from previous game winner). And we have S[i] is the server for rally i.
We need to compute a and b given S. This is essentially a deterministic reconstruction because we can derive most winners from the server transitions. The only unknown is the winner of the last rally. But perhaps we can also deduce some constraints from the serve sequence length. Let's denote N = length of serve sequence string.
We can compute:
For i from 0 to N-2: W[i] = S[i+1].
Thus we know the winners of all but the last point. Let a_known = count of A among W[0..N-2], b_known = count of B among W[0..N-2]. Then the final point winner W_last is either A or B. So final scores: a = a_known + (W_last == A), b = b_known + (W_last == B). Also we know that the initial server S[0] is given. And we can also verify consistency: The server for each rally should be consistent with the previous winner? Actually we already used that to derive W[i] from S[i+1]. So that's consistent by construction. However, we also need to ensure that the server for the first rally is indeed the initial server as per game rule. That's fine.
Now we need to find which W_last leads to a valid game end condition. Typically only one of the two possibilities will yield a valid ending where the game ends exactly after that point and not earlier.
But we must also ensure that before the last point, the game was not already over. So we need to check that after each point, the condition for game end is false until the final point. Since we know all winners except the last, we can simulate scores incrementally, and at each step (after each point) check if the game would have ended earlier. If with a particular candidate for W_last, the game would have ended at an earlier point (i.e., after some point before the last, the winning condition is met), then that candidate is invalid because the game would have stopped then, and the serve sequence would not include later serves. So we need to find W_last such that after processing points 0..N-1 (with W[N-1] = candidate), the game ends only after the last point, i.e., for all i from 0 to N-2, after point i the game is not over; after point N-1, it is over.
Thus we can try both possibilities: assign W_last = A or B, compute final scores, and check if the game ending condition holds after the last point, and that it does not hold after any previous point. If exactly one candidate satisfies, we output that score. But is it possible both satisfy? Possibly not, due to the constraints. However, we must be careful: there might be cases where the game could have ended earlier but the serve sequence length suggests it didn't; thus only the candidate that avoids early end will be valid.
But wait: Could there be cases where both candidates are invalid? That shouldn't happen if the input is consistent. But we must ensure our algorithm doesn't break.
One nuance: The initial server for game2 and game3 depends on the winner of the previous game. So we need to know the winner of game1 before processing game2, and similarly for game3. That is straightforward: after we compute game1 scores, we know who won (the one with 11 or more, and higher). Then we set initial server for game2 as that winner. Then process game2 serve sequence accordingly. But what if the game1 winner is ambiguous? Actually it should be determined uniquely.
Now, we must also consider that the serve sequence for each game is given as a string. But there is a nuance: The record may include serves after the game ended? No, it's the serve order of the game, so it stops when game ends. So we can rely on that.
Thus approach: For each dataset, for each of the three games (i=1,2,3) in order:
- Determine initial server: For game1: "A". For game2: winner of game1. For game3: winner of game2.
- Get the serve sequence string s.
- Let N = len(s)
- We need to compute the score (a, b) from s and initial server.
We'll do: Create list servers = characters of s.
Let initial = servers[0]. According to rules, the first server must match the rule. For game1, servers[0] should be 'A'. For game2, servers[0] should be winner of game1. But the input is given; we should assume it's consistent. But we may also verify? Possibly not required; just trust input.
Now, we know for i from 0 to N-2: winner_i = servers[i+1] (since next server is winner of current rally). So we can compute points for first N-1 rallies.
But we need to be careful: The rule says "the next serve is done by the person who got the previous point." So indeed, the server for the next rally is the winner of the current rally. So our deduction is correct.
Thus we can compute counts a_known, b_known from winners_0 to winners_{N-2}. Then we need to find the winner of the last rally (W_last). We'll try both possibilities.
But we must also ensure that the server for the last rally (servers[N-1]) is consistent with who served that rally, but we don't know who won that rally from server transition (since there is no next serve). However, there is also a possible constraint: The server for the first rally is given; for subsequent rallies, server_i = winner_{i-1} (since the server for rally i is the winner of previous rally). So we can also deduce the consistency for all i>=1: servers[i] should equal winner_{i-1}. That is exactly the relation we used. So that's already satisfied for i from 1 to N-1? For i from 1 to N-1, we have servers[i] = winner_{i-1}. We set winner_{i-1} = servers[i] for i from 1 to N-1? Actually we set winner_{i-1} = servers[i] for i=1..N-1. This uses all servers except the last? For i = N-1, servers[N-1] is used as winner_{N-2}. So that's fine. For the last rally, we need to know winner_{N-1}. But we also have that the server for rally N-1 is servers[N-1]; that must be equal to winner_{N-2} (which is servers[N-1] from i=N-1? Wait: Let's check indexing.
Define rally index i from 0 to N-1.
server_i = servers[i] (given).
After rally i, winner_i determines next server: server_{i+1} = winner_i, for i < N-1.
Thus for i from 0 to N-2, we have winner_i = server_{i+1}. So winners for i=0..N-2 are known.
For i = N-1, winner_{N-1} is unknown, and there is no server_{N} to compare.
Thus consistency is automatically satisfied by construction.
Now to determine winner_{N-1}, we need to consider that after rally N-1, the game ends. So the score after adding winner_{N-1} must satisfy the winning condition, and before that (i.e., after rallies 0..N-2) the condition must not be satisfied.
Thus we simulate points sequentially for i=0..N-2 with known winners, track scores a_curr, b_curr. At each step, after updating, we check if game_end(a_curr, b_curr) is true. If it ever becomes true before last point, then the candidate (any) would be invalid because the game would have ended earlier. But wait: That check depends only on known winners; it's independent of the last winner. So for the sequence to be valid, after the known N-1 points, the game must not have ended. So we require that after processing all known winners, the game is not over. That is a necessary condition for both candidates to be potentially valid. If after N-1 points the game is already over, then the record would have stopped earlier, so the input would be inconsistent. But we assume it's consistent, so after N-1 points, the game should not be over.
Then we consider adding the last point with either A or B as winner. For each candidate, we compute final scores and check if game_end is true. Also need to ensure that the server for the last rally? Already given, but we might also need to check that there is no additional constraint like the server for the last rally must be consistent with the fact that the winner of the previous point is server for this rally? That's already ensured for all i from 1 to N-1 (including last rally) because we have server_i = winner_{i-1} for i>=1. For i=N-1, we have server_{N-1} = winner_{N-2} (which we know). That is satisfied automatically because we used winner_{N-2}=servers[N-1] (since servers[N-1] is the given server for last rally, and it must equal winner_{N-2}). Wait, careful: For i=N-1, the equation server_{N-1} = winner_{N-2} is necessary. But we have defined winner_{N-2} = servers[N-1]? Actually from i = N-2, we have winner_{N-2} = server_{N-1} (since server_{N-1} is the next server). So yes, that equation holds. So there's no extra constraint.
Thus we just need to try both candidates for winner_{N-1}.
But is there any other nuance? Possibly the game could end exactly at 11-0 or 0-11, which is valid if the difference is 2? Actually 11-0 difference is 11, so it's fine. Also after 10-10, need win by 2, so scores can be higher than 11. So winning condition: Let maxScore = max(a,b). The game ends when (maxScore >= 11) and (abs(a-b) >= 2). And also only after that point the game ends; before that, these conditions are not met.
But wait: There's also the possibility that before 10-10, a player reaches 11 with a difference of at least 2, that ends the game. After 10-10, they must win by 2, so it continues until one leads by 2. So the condition as described works.
Now, we need to compute the score for each game.
But there is a subtlety: The initial server for game2 is "the person who took the previous game makes the first serve". So if game1 winner is A, then initial server for game2 is A. That must match the first character of the serve sequence for game2. We can rely on input being consistent.
Now, we need to think about the case where after known N-1 points, the game is not over. Could both candidates produce a valid ending? Let's test. Suppose after N-1 points, scores are (10,10). Then the game is not over because difference is 0 and neither has 11 with lead? Actually at 10-10, it's not over because need win by 2. So it's not over. Then if last point winner is A, final scores (11,10) - but that does not meet win by 2, so the game would not end; it would continue. So that candidate would be invalid because game would not end after that point. If last winner is B, (10,11) similarly invalid. So both invalid. But then the game would not end after just one more point; it would require at least two more points. However, our record length is N, meaning only one more point was played. So such scenario can't happen if input is consistent. So we need to ensure that the last point leads to a valid end. So only candidate that makes the game end will be valid.
Consider after N-1 points scores (10,9). Then if last winner is A: (11,9) -> difference 2, game ends. If last winner is B: (10,10) -> not ended. So only A valid.
Consider (9,10): similar.
Consider (10,8): If last winner is A: (11,8) ends; B: (10,9) not ends.
Consider (9,9): If last winner A: (10,9) not ends; B: (9,10) not ends. So both invalid, but then N would be longer? Actually if after N-1 points scores (9,9), the game hasn't ended, and one more point cannot end it (since need to reach at least 11 with difference 2). So N-1 cannot be the second-last point if N is the total points, because the game would require at least 2 more points. So such a state cannot occur if the game ends exactly after N points. So it won't happen.
Thus exactly one candidate should be valid.
But we must also consider the possibility that after N-1 points, the game is already over. That would be invalid because the game would have stopped earlier. But maybe the input could include serves beyond the end? No, the record is of the game, so it should stop at the end. So we can assume it's not.
Thus algorithm is straightforward.
Now, one more nuance: The record string length could be zero? Possibly not, because a game must have at least 1 serve? Actually a game could be 0-0? No, game must have at least one point? Actually minimal game is 11-0 or 0-11, which requires at least 11 points. So length >= 11. But we don't need to assume.
Now, we need to process multiple datasets until a line containing "0". The datasets consist of three lines each (three strings). The number of datasets is up to 20. The input ends with a line containing "0" (alone). But note: the example input shows 6 lines then 0. So that's two datasets.
Now, confirm with example: Input first dataset lines:
- ABAABBBAABABAAABBAA
- AABBBABBABBAAABABABAAB
- BABAABAABABABBAAAB
Let's compute for the first line to see if we get 11 8.
We'll simulate manually to test.
Game1: initial server A. Serve sequence: "A B A A B B B A A B A B A A A B B A A"? Actually the string: "ABAABBBAABABAAABBAA". Let's list characters with index.
s = A B A A B B B A A B A B A A A B B A A? Wait length? Let's write: indices 0..len-1. The string: "A B A A B B B A A B A B A A A B B A A"? Count: A B A A B B B A A B A B A A A B B A A? That seems 20 characters? Let's count properly: "ABAABBBAABABAAABBAA". Write out:
0:A
1:B
2:A
3:A
4:B
5:B
6:B
7:A
8:A
9:B
10:A
11:B
12:A
13:A
14:A
15:B
16:B
17:A
18:A? Actually after 15: B, 16: B? Wait need accurate.
Better: "ABAABBBAABABAAABBAA"
Let's group: A B A A B B B A A B A B A A A B B A A? Actually let's count length:
String: "ABAABBBAABABAAABBAA"
Indices:
0 A
1 B
2 A
3 A
4 B
5 B
6 B
7 A
8 A
9 B
10 A
11 B
12 A
13 A
14 A
15 B
16 B
17 A
18 A? Wait after 17: index 18? Let's check the string: "ABAABBBAABABAAABBAA". Write it with positions:
1 A
2 B
3 A
4 A
5 B
6 B
7 B
8 A
9 A
10 B
11 A
12 B
13 A
14 A
15 A
16 B
17 B
18 A
19 A? Actually the string length is? Count characters:
A B A A B B B A A B A B A A A B B A A - that's 20 characters. But the string "ABAABBBAABABAAABBAA" appears to have 19? Let's count using Python mentally: "A"=1, "B"=2, "A"=3, "A"=4, "B"=5, "B"=6, "B"=7, "A"=8, "A"=9, "B"=10, "A"=11, "B"=12, "A"=13, "A"=14, "A"=15, "B"=16, "B"=17, "A"=18, "A"=19? That's 19? Actually after "A" at pos13,14,15? Let's be precise: string: ABAABBBAABABAAABBAA
Break it: A B A A B B B A A B A B A A A B B A A? Let's count letters:
1 A
2 B
3 A
4 A
5 B
6 B
7 B
8 A
9 A
10 B
11 A
12 B
13 A
14 A
15 A
16 B
17 B
18 A
19 A? That gives 19 letters? But I count 19? Actually 1-19 is 19. But maybe it's 20: Check the substring: "ABAABBBAABABAAABBAA". Could also be: ABAABBBAABABAAABBAA. Let's count by grouping:
ABA (3)
ABB (6?) Not helpful.
Better to count programmatically but as I'm not running, I'll trust it's length N. But the algorithm should work.
Now we need to compute scores. Let's try to simulate:
We know initial server = 'A' (for game1). For each rally i (0-indexed), server_i = s[i]. Winner_i = s[i+1] for i < N-1. For last rally, unknown.
We can compute known winners.
Let's list s: index and server.
I'll represent s as list:
s[0] = A
s[1] = B
s[2] = A
s[3] = A
s[4] = B
s[5] = B
s[6] = B
s[7] = A
s[8] = A
s[9] = B
s[10] = A
s[11] = B
s[12] = A
s[13] = A
s[14] = A
s[15] = B
s[16] = B
s[17] = A
s[18] = A? Wait we need to check length. Let's count by actually writing the string: "ABAABBBAABABAAABBAA". Perhaps I'll split: "AB A A B B B A A B A B A A A B B A A"? That seems off.
I'll count letters manually:
1:A
2:B
3:A
4:A
5:B
6:B
7:B
8:A
9:A
10:B
11:A
12:B
13:A
14:A
15:A
16:B
17:B
18:A
19:A? The string ends with "AA"? The given string ends with "AA" indeed: " ... BBAA". So length 19? But let's count from start: "A" (1), "B" (2), "A" (3), "A" (4), "B" (5), "B" (6), "B" (7), "A" (8), "A" (9), "B" (10), "A" (11), "B" (12), "A" (13), "A" (14), "A" (15), "B" (16), "B" (17), "A" (18), "A" (19). So length 19. But could there be a missing character? Actually the output says "11 8", so final scores 11-8 means total points 19, which matches length 19. Because total points = a + b = 11+8 = 19. So N=19. Good. So length matches total points.
Now we can compute known winners for first 18 rallies (i=0..17). For each i from 0 to 17, winner_i = s[i+1].
Thus winners list (length 18) = s[1] through s[18]. s[1] to s[18] are: index1 B,2 A,3 A,4 B,5 B,6 B,7 A,8 A,9 B,10 A,11 B,12 A,13 A,14 A,15 B,16 B,17 A,18 A. Let's list:
i=0: winner = s[1]=B
i=1: winner = s[2]=A
i=2: winner = s[3]=A
i=3: winner = s[4]=B
i=4: winner = s[5]=B
i=5: winner = s[6]=B
i=6: winner = s[7]=A
i=7: winner = s[8]=A
i=8: winner = s[9]=B
i=9: winner = s[10]=A
i=10: winner = s[11]=B
i=11: winner = s[12]=A
i=12: winner = s[13]=A
i=13: winner = s[14]=A
i=14: winner = s[15]=B
i=15: winner = s[16]=B
i=16: winner = s[17]=A
i=17: winner = s[18]=A
So winners (first 18) = B, A, A, B, B, B, A, A, B, A, B, A, A, A, B, B, A, A.
Now let's simulate scores after these 18 points, starting from 0-0. We'll keep a and b.
Initialize a=0,b=0.
i=0: winner B -> b=1
i=1: winner A -> a=1
i=2: winner A -> a=2
i=3: winner B -> b=2
i=4: winner B -> b=3
i=5: winner B -> b=4
i=6: winner A -> a=3
i=7: winner A -> a=4
i=8: winner B -> b=5
i=9: winner A -> a=5
i=10: winner B -> b=6
i=11: winner A -> a=6
i=12: winner A -> a=7
i=13: winner A -> a=8
i=14: winner B -> b=7
i=15: winner B -> b=8
i=16: winner A -> a=9
i=17: winner A -> a=10
After 18 points, scores: a=10, b=8. That's (10,8). Good.
Now after these 18 points, game is not over? Condition: a=10,b=8, max=10, difference=2. But is 10 enough to win? No, need at least 11. So not over.
Now the last point (i=18) has server s[18]=A (since s[18] is the last character). But we don't know winner of last rally. To end the game, we need final scores such that someone wins. Since a=10,b=8, if last winner is A: a=11,b=8 -> 11 with difference 3 => game ends. If last winner is B: a=10,b=9 -> still no one reached 11, so game not ended. So the only valid candidate is A wins last point. Thus final scores: 11-8. That matches output.
Thus algorithm works.
Now second game in first dataset: record "AABBBABBABBAAABABABAAB". Let's compute length. Might be 22? Output says "10 12". That's total 22 points. So length 22. We'll trust algorithm.
Now need to implement reading input. Input consists of multiple datasets, each with 3 lines (records). The termination is a line containing "0". However, it's possible that some lines could be empty? Probably not. We'll read lines until we encounter a line equal to "0". But note that dataset lines are non-empty strings of A/B, and they can be of any length up to 100.
Thus we can read all lines into a list, and then iterate grouping by 3 until we hit "0". But careful: The "0" line could appear after a multiple of 3 lines? In example, after 6 lines, we get "0". So we can read lines until we encounter "0". Then we know number of lines before "0". That should be divisible by 3 (since each dataset has 3 records). So we can process for i in range(0, len(lines), 3).
Alternatively, we can read one line at a time, collecting three strings, then if a line is "0", we stop (but if we have collected a partial dataset? The problem statement likely ensures data integrity.
We'll implement as:
while True:
line1 = input().strip()
if line1 == "0":
break
line2 = input().strip()
line3 = input().strip()
Process dataset with these three strings.
But caution: There might be blank lines? The problem says each dataset is given as three lines, each a string. So reading line by line should be fine.
Now we need to compute score for each game.
Implementation steps for compute_score(serve_sequence, initial_server_expected) -> (a, b). But we also need to verify that initial_server_expected matches the first character of serve_sequence? Possibly we could just assume it's consistent. But we need to set the initial server for simulation. However, the serve_sequence string itself provides the server for each rally. The first character is the server of the first rally. According to the rules, that must equal the expected initial server. So we could just take the first character as initial server. But for game2 and game3, we need to know who the initial server should be based on previous winner. So we can compare if it matches; if not, maybe input inconsistent, but we just proceed using the given first character? Actually the problem says "the record of the serve order is represented by the character string 'A' or 'B' representing the person who hit the serve." It doesn't say we need to verify consistency with the rules; we just need to compute the score. But the initial server for each game is determined by the rules, but that might be implicitly reflected in the record: i.e., the first character of the record should be the initial server. So we can just use that as the initial server. However, we also need to know the winner of previous game to compute the initial server for game2? Actually we don't need to externally set the initial server; we can just use the first character of the record as the server for the first rally. That's fine because the record is the sequence of servers, and that includes the first serve. So we don't need to know who the initial server should be; we can just simulate based on the given serve sequence. The initial server is simply s[0]. But wait: The rule about initial server for game2 says "the person who took the previous game makes the first serve." This is a rule that must be satisfied by the recorded serve sequence. So if we just use s[0] as the server, we are effectively trusting that it matches the rule. But we need to compute the score of the game, which doesn't depend on the rule; we just need to deduce points from the serve sequence. The only relation we need is that the winner of previous game is the server of the first rally of next game. But we don't need that to compute the scores; we only need to compute the score of the current game using its serve sequence. However, there is a subtle point: The deduction of winners from serve sequence uses the principle that the next server is the winner of the current rally. This principle holds irrespective of the initial server. So for any game, given the serve sequence, we can deduce all winners except the last. The initial server is just s[0]. So we can compute the scores for a game without needing to know who should be initial server according to previous game. So we can process each game independently! Wait, but is there any dependency? The rule about initial server for game2 is about who serves first, which is encoded in the serve sequence. So as long as we have the serve sequence, we can compute the scores for that game without knowledge of previous game outcome. The only reason we might need previous game winner is to confirm consistency, but not for score calculation. However, there is a nuance: For game2, the first serve must be by the winner of game1. But if we compute the score of game1 first, we can check if the first serve of game2 matches that winner, but if it doesn't, maybe the input is inconsistent. But the problem might assume consistent input.
But note: The description says: "In the 2nd and 3rd games, the person who took the previous game makes the first serve." This is part of the rules that govern the game. So the recorded serve order should adhere to that. However, when reconstructing scores, we can still deduce winners from serve sequence independently. There is no need to know who should serve first; the serve sequence itself tells us who served first. So we can compute scores directly.
But wait: Could there be a situation where the initial server of a game is not consistent with the winner of previous game? In that case, the input would be inconsistent. But the problem likely guarantees consistent data. So we can compute each game independently.
But careful: The algorithm to deduce scores from serve sequence depends only on the serve order and the rule that server changes to winner. That is universal. So we can process each record in isolation.
Thus we can treat each dataset as three independent game records. However, we need to output the score for each game. So we simply compute score for each string.
But is there any nuance about the game ending condition? The game ends when a player gets 11 points first with a difference of 2, or after deuce when difference 2. So our simulation of ending condition is correct.
Now, is there any scenario where the last rally's winner cannot be determined uniquely because both candidates could produce a valid end? Let's think more carefully. Suppose after N-1 points scores are (10,10). Then both A and B last point would make (11,10) or (10,11), neither ends game. So both invalid. So not possible. Suppose after N-1 points scores are (11,10). That would already be a winning condition? That would mean game already ended, so N-1 points would have ended game, so N would be N-1? But the record length is N, so if after N-1 points the game already ended, then the game would have stopped, and there would be no Nth serve. So that can't happen. Similarly, (10,11) etc. So only one candidate should work.
What about (9,10) and (10,9) as above. That yields unique.
Consider (10,10) is invalid for both. So only one candidate works.
What about (9,11)? That would already be end? Actually 11 points with difference 2? 11-9 difference 2, so game would have ended at that point. So that can't be after N-1 points because game would have ended. So not.
What about (12,10) after N-1? That would be end already. So invalid.
Thus unique.
Edge case: If the game ends exactly when someone reaches 11 with a difference of 2, and the last point is served by the winner or loser? We don't care; the serve sequence includes that last serve. The next server would be the winner, but since game ends, there is no next serve. So the record ends exactly after that serve. Our method will find that candidate winner must be the one that makes game end. So consistent.
Now, what if the game ends after deuce: e.g., scores go to 12-10. Then total points 22. The serve sequence length = 22. After 21 points we might have (11,10) or (10,11) which is not end. Then last point yields 12-10 or 11-11? Actually 11-10 is not end; if last point winner is leading player, then 12-10 ends; if the other, then 11-11, not end. So unique.
Thus algorithm is solid.
Now, implement function compute_score(serve_str): return (a,b).
Procedure:
- N = len(serve_str)
- If N == 0? Not possible. But handle.
- servers = list of chars.
- known_winners = servers[1:] // because for i=0 to N-2, winner_i = servers[i+1]
But careful: known_winners length = N-1.
Now we simulate scores for the first N-1 rallies:
Initialize a=0,b=0.
For each winner in known_winners:
if winner == 'A': a+=1 else b+=1.
Check after each addition if game has ended (i.e., (a>=11 or b>=11) and abs(a-b)>=2). If at any point it ends, then this record is inconsistent (should not happen). But we can ignore as input is consistent.
Now we have a_pre, b_pre.
Now try candidate for last winner:
Option1: last_winner = 'A' => a_final = a_pre+1, b_final = b_pre
Option2: last_winner = 'B' => a_final = a_pre, b_final = b_pre+1
Now check which one yields a valid ending: i.e., (a_final>=11 or b_final>=11) and abs(a_final - b_final) >=2. Also need to ensure that the game was not already ended before. However, we already know a_pre,b_pre does not satisfy ending condition (should not). But we could double-check that the game would not have ended earlier. Actually we must also ensure that before the last point, the game was not over. So we need to verify that after processing first N-1 points, the game was not over. If it were over, then the record would be invalid. So we should check that for the correct candidate, after N-1 points the game is not over. So we can test both candidates; whichever satisfies ending condition and also ensures that before last point (a_pre,b_pre) is not a winning state, that is the answer. But could both candidates lead to ending condition? Possibly if a_pre,b_pre already is a winning state? Then both would also be winning? Actually if a_pre,b_pre already winning, then adding any point would still be winning? But the game would have ended earlier, so no further serves. So that state would be invalid for both. So we can just choose the candidate that yields winning final state, and also require that a_pre,b_pre is not winning. However, it's possible that both candidates yield winning final state? Suppose a_pre=10,b_pre=10, then final with A: 11,10 (not winning because diff=1), with B:10,11 (not winning). So not both. Suppose a_pre=11,b_pre=9 (winning already) then adding A:12,9 (winning), adding B:11,10 (winning? 11-10 diff=1, not winning). So only one yields winning? Actually if a_pre=11,b_pre=9, it's already winning, so game should have ended, so length would be N-1, not N. So both invalid. So unique.
Thus we can just compute both, check which final state is winning. There will be exactly one.
But we need to ensure that we also check the condition "After 10-10, the winner will be the one who has a difference of 2 points." Our condition (max>=11 and diff>=2) covers that because if both are 10-10, max=10<11 so not winning; after 11-10, diff=1 so not winning; after 12-10, max=12>=11 and diff=2 => winning. So correct.
Now we must consider the case where the game ends at exactly 11-0, 0-11. That works.
Now, we should also consider the possibility that the serve sequence includes the very first serve but no points? Not possible.
Implementation steps for each dataset:
- Read three strings s1, s2, s3.
- For each string s, compute score.
- Print each score on its own line (within dataset output, three lines).
- After processing dataset, continue to next.
Now, check the example output: They printed 6 lines, which correspond to three games per dataset? Actually example output shows:
11 8
10 12
11 7
11 8
11 0
0 11
So first dataset outputs 3 lines: 11 8, 10 12, 11 7? Wait lines: 11 8 (first game), 10 12 (second game), 11 7 (third game). Second dataset: 11 8, 11 0, 0 11? That's three lines: 11 8, 11 0, 0 11. But output shows 11 8, 10 12, 11 7, 11 8, 11 0, 0 11. So that's 6 lines total. So first dataset: line1: 11 8, line2: 10 12, line3: 11 7. Second dataset: line4: 11 8, line5: 11 0, line6: 0 11.
Now verify our algorithm on the second dataset's second game: "AAAAAAAAAAA" length 11. Compute known winners: from indices 0..9, winners = servers[1:11] = all 'A'? The string is all A's. So servers[0] = A, servers[1..10] = A. So known winners = all A (10 winners). So after 10 points, a=10, b=0. Not yet ended? 10-0 diff 10 but max<11, so not ended. Last point candidate: if A wins, a=11,b=0 ends; if B wins, a=10,b=1 not ends. So final 11-0. Correct.
Third game: "ABBBBBBBBBB" length? Actually given "ABBBBBBBBBB" that's A followed by 10 B's? Let's count: "A" + 10 B's? Actually string "ABBBBBBBBBB": first A, then 10 B? The length should be 11? Because total output is 0 11. So length 11. So servers: s[0]=A, s[1..10]=B. Known winners = s[1..10] = all B (10 winners). So after 10 points, a=0,b=10. Not ended. Last point: if B wins, b=11 ends; if A wins, a=1,b=10 not ends. So final 0-11. So matches.
Now, note about game2's first dataset: output 10 12. That means A=10, B=12. So total points 22. That suggests a deuce scenario where B wins 12-10. Let's test quickly: record "AABBBABBABBAAABABABAAB". We can trust our algorithm.
Now, consider edge cases: What if the length is such that after N-1 points the game is not over, and both candidates yield a winning state? Could that happen? Let's check. For both to be winning, the final scores after adding either A or B must both satisfy (max>=11 and diff>=2). Suppose a_pre = 10, b_pre = 10. Then final A: 11,10 (not winning because diff=1). final B:10,11 (not). So no.
Suppose a_pre = 11, b_pre = 10 (already winning). Then final A:12,10 (winning) and final B:11,11 (max=11,diff=0 => not winning). So only one.
Suppose a_pre = 12, b_pre = 10 (winning already). final A:13,10 (winning), final B:12,11 (max=12,diff=1 not winning). So only one.
Suppose a_pre = 11, b_pre = 9 (winning already) final A:12,9 (winning), final B:11,10 (not). So only one.
Suppose a_pre = 9, b_pre = 9 final both not winning.
Thus unique.
Therefore we can safely pick the one that yields a valid end.
But we should also ensure that after N-1 points the game is not over, because if it were over, then both candidates would be invalid? Actually if after N-1 points it's already winning, then no candidate can be valid because the game would have ended earlier, but perhaps the record could still have that extra serve? Not according to rules. So we assume consistency.
Now, there is one more nuance: The serve sequence is the sequence of servers. The deduction that winner_i = server_{i+1} relies on the rule "the next serve is done by the person who got the previous point." However, is this rule always true? Yes, according to problem statement: "the next serve is done by the person who got the previous point." So that's correct.
Thus we have a straightforward algorithm.
Now, we must consider if there are any special cases where the first serve is not necessarily the first character? For example, before the first point, there is a serve. That's s[0]. Good.
Now, implementation details:
Function game_score(record):
N = len(record)
# Known winners: all but last
winners_known = [record[i+1] for i in range(N-1)]
a = 0
b = 0
# simulate known points
for w in winners_known:
if w == 'A':
a += 1
else:
b += 1
# Optional: check if game ended prematurely? Could raise error.
# Try last winner
# candidate A
aA = a + 1
bA = b
if (aA >= 11 or bA >= 11) and abs(aA - bA) >= 2:
# also check that before last point game was not over; we assume it's not.
return (aA, bA)
else:
# candidate B
aB = a
bB = b + 1
# Should be valid
if (aB >= 11 or bB >= 11) and abs(aB - bB) >= 2:
return (aB, bB)
else:
# Should not happen; maybe handle error.
# Could also fallback to something.
return (0,0)
But wait, we must also consider the case where after known points, the game is not over, but both candidates might not satisfy the ending condition? That would be inconsistent. We'll trust input.
Now, check the second dataset's second game: "AAAAAAAAAAA" length 11. winners_known = record[1:11] = all 'A', so a=10, b=0 after 10 points. aA=11, bA=0 -> (11>=11, diff=11>=2) => true, so returns (11,0). Good.
Second dataset third game: "ABBBBBBBBBB": length 11. winners_known = record[1:11] = all 'B', a=0, b=10. aA=1,bA=10 -> not winning (max=10<11). So else: aB=0,bB=11 -> winning, returns (0,11). Good.
Now, what about deuce scenario like 12-10? Example: second game output is 10 12, meaning A=10, B=12. So total points 22. Let's test with record "AABBBABBABBAAABABABAAB" length 22? We'll compute quickly: length should be 22. We'll simulate: record length = 22, so known winners length = 21. Compute a_pre, b_pre. Then test last winner. Should yield B=12. We'll trust.
Thus the algorithm is correct.
Now, confirm we don't need to know initial server for each game separately. It's embedded.
Now, the problem might have a twist: The serve order includes the serve of the point that ends the game. Indeed.
Now, confirm no other interpretation: Could there be a case where the server for the next rally is not necessarily the winner? In standard rally scoring, yes. Problem explicitly says: "the next serve is done by the person who got the previous point." So our mapping is correct.
Now, edge cases: The serve sequence might be very long, up to 100. That's fine.
Now, we must also consider that the game might have ended exactly at the moment when a player reaches 11 with a difference of 2, but after 10-10 they must win by 2. So our condition covers that.
Now, there is also a rule: "3 Play the game." Possibly means they play best of 3 games. That's just context.
Now, one more nuance: In badminton, the serve changes depending on who wins the point, but also there is a concept of serving from specific side, but irrelevant.
Thus solution is ready.
Now, we need to implement reading multiple datasets. The input format: multiple datasets; each dataset consists of three lines, then next dataset, etc., terminated by a line containing "0". There could be blank lines? Probably not.
We'll implement a while loop:
def main():
import sys
lines = sys.stdin.read().strip().splitlines()
# Process lines until we encounter "0"
i = 0
outputs = []
while i < len(lines):
line = lines[i].strip()
if line == '0':
break
# dataset: next three lines (including this line)
rec1 = line
rec2 = lines[i+1].strip()
rec3 = lines[i+2].strip()
i += 3
# compute scores for each
for rec in (rec1, rec2, rec3):
a,b = compute_score(rec)
outputs.append(f"{a} {b}")
print("\n".join(outputs))
But careful: The input might have empty lines; we can strip them. The problem says each dataset is given as three lines, each a string. So we can read sequentially.
Alternatively, we could read line by line using sys.stdin.
I'll use:
import sys
def compute_score(s):
N = len(s)
# known winners
a = b = 0
for i in range(N-1):
if s[i+1] == 'A':
a += 1
else:
b += 1
# try A as last winner
if (a+1 >= 11 or b >= 11) and abs((a+1)-b) >= 2:
return (a+1, b)
else:
return (a, b+1)
But we need to ensure that we are not missing the case where both candidates satisfy condition? Could that happen? Let's test a scenario: after N-1 points, a_pre = 10, b_pre = 9. Then a+1=11,b=9 => (11>=11, diff=2) => true. a,b+1=10,10 => (max=10<11) => false. So only one.
What about a_pre = 9, b_pre = 10: a+1=10,10 false; a,b+1=9,11 => (11>=11, diff=2) => true. Good.
What about a_pre = 11, b_pre = 9? That can't happen because that would already be winning, but if it did, a+1=12,10 true; a,b+1=11,10 false. So only one.
Thus we can safely do if-else.
Now, we should ensure that the condition is correct: need both (max>=11) and (diff>=2). But note that if a=11, b=9 diff=2, that's winning. So we need both. Our condition (a+1 >= 11 or b >= 11) and abs((a+1)-b) >= 2. Actually if a+1 >= 11 or b >= 11, but both could be true. For example, a+1=12, b=11: both >=11 and diff=1? That would be 12-11 diff=1, not win. In that case the condition (