We need to read problem carefully. This is about 1D-Go. We have board of length L, represented by string S of characters B, W, .. White player is to move, can place a white stone on an empty cell ( '.'). However, there is a restriction: the player cannot make a chain of white stones which is surrounded by black stones even if it simultaneously makes some chains of black stones be surrounded. That is, if after placing a white stone, there exists a contiguous segment of white stones that is flanked on both sides by black stones (or board edge? Let's read definition of capture: "if i-th grid and j-th grids (1 < i+1 < j ≤ L) contain white stones and every grid of index k (i < k < j) contains a black stone, these black stones will be captured, and vice versa about color." So capture occurs when there is a segment of black stones entirely between two white stones (or white stones on both ends). Similarly for white stones between two black stones. However note that capture only applies when a chain is surrounded by opponent's stones. Also note that the board edges: edges are not stones, so a chain adjacent to edge is not surrounded because there is no opponent stone on that side. This is clarified by statement: "The player cannot capture black stones by surrounding them with the edge of the game board and the white stone. This rule is also different from the original Go." So edges don't count as capturing boundaries.
Now, the white player can place a white stone, but cannot place it such that the resulting board has a white chain that is surrounded by black stones. That means after the move, there should be no contiguous white segment that has black stones on both its immediate left and right (i.e., the positions adjacent to the segment). But careful: if the white chain is at the edge? e.g., if white stone placed at leftmost cell, then left side is board edge, not a black stone, so it's not surrounded. So the restriction only applies if after placement, there is some white chain that is flanked by black stones (meaning the positions immediately to the left and right of the chain are either black stones or perhaps board edges? but edges are not stones, so not considered surrounding? The phrase "surrounded by stones with opponent's color" in the definition of capturing. But the restriction says: "the player can not make a chain of white stones which is surrounded by black stones even if it simultaneously makes some chains of black stones be surrounded." So this refers to the condition that would cause that white chain to be captured? Actually in normal game, if you place a stone that creates a chain that is surrounded, that chain would be captured (by opponent's stones). But here they prohibit making a white chain that is surrounded by black stones, regardless of capture? Possibly they are saying it's an illegal move if after placing the white stone, there exists a white chain that is surrounded by black stones (i.e., that would be captured immediately according to the capture rule, but they say "even if it simultaneously makes some chains of black stones be surrounded", meaning normally in Go you could play a suicidal move if it captures opponent stones? But here it's illegal to make a suicidal move). They explicitly state "the player can not make a chain of white stones which is surrounded by black stones". So we must only consider moves that do not produce a white chain that is surrounded by black stones.
Also it's guaranteed that given state has at least one legal move (i.e., there is at least one '.' where placing white does not create a surrounded white chain, and also that currently there are no chains already surrounded (so no capture immediately possible before move). Also the board length up to 100, so we can brute-force all possible moves.
We need to compute the maximum number of black stones that can be captured by the white player's next move, among all legal moves (i.e., placements that do not create a surrounded white chain). The capture occurs after placing the white stone; we need to count the number of black stones that get captured according to the rule: any black chain that is surrounded by white stones on both sides (i.e., the black segment is completely between two white stones, with no empties, and the white stones are adjacent to the ends of the black segment). More precisely, if there exist indices i and j (i < j) such that S[i] = 'W', S[j] = 'W', and for all k in (i, j) S[k] = 'B', then those black stones are captured. And note that if there are multiple such segments, they all are captured. Also note that white stones placed may create multiple captures possibly. Also need to consider that after placement, the capture of black stones may happen; do we also consider that captured stones are removed? But for counting, we just need the number of black stones captured. The problem statement: "computes the maximum number of black stones which can be captured by the next move of the white stones player." So we count the black stones that become surrounded by white stones after the move.
Now, we need to check the legality of a move: After placing white at some position p (currently '.'), we need to ensure that no white chain is surrounded by black stones. A white chain is a maximal contiguous segment of W's. A white chain is surrounded by black stones if the cells immediately to its left and right (if they exist) are black stones (i.e., 'B'). But careful: what about the edges? If chain extends to edge, then left side or right side is absent, so not surrounded. Also if adjacent cell is empty '.'? Then not surrounded. So the condition for a white chain to be surrounded is: (left neighbor exists => that cell is 'B') and (right neighbor exists => that cell is 'B'). However, is there any nuance about multiple chains? Possibly also if white chain is adjacent to a black stone on both sides but also there might be empty cells? Actually surrounded means both sides are opponent stones. In capture definition: "if i-th grid and j-th grids (1 < i+1 < j ≤ L) contain white stones and every grid of index k (i < k < j) contains a black stone" This describes a scenario where two white stones at i and j enclose a block of black stones. The white chain could be more than one stone? The definition says "chain" is maximal continuous stones of same color. For capture of black stones, you need white stones at both ends of the black segment; but those white stones could be part of longer white chains? Actually if we have a white chain, say a contiguous block of white stones from i to j, then for it to be surrounded by black stones, there must be a black stone immediately to the left of i (if i>1) and immediately to the right of j (if j<L). But wait: The definition of capture for white stones is analogous: if there are black stones at i and j, and all between are white stones, then those white stones are captured. So a white chain is surrounded if there exist black stones at both ends that directly neighbor the chain? But the capture condition would require that the chain is entirely between two black stones, with no gaps. That means that if there is a white chain from indices a to b, then it is surrounded (and thus captured) if there exists i < a and j > b such that S[i] = 'B', S[j] = 'B', and all indices between i+1 to a-1 are ??? Actually the capture condition: "if i-th grid and j-th grids (1 < i+1 < j ≤ L) contain white stones and every grid of index k (i < k < j) contains a black stone, these black stones will be captured, and vice versa about color." For white stones captured: there would be i and j with black stones, and all between are white. So it's essentially that a chain of white stones is captured if it's flanked immediately by black stones on both sides? For a chain from a to b to be captured, we need that the positions immediately outside the chain (a-1 if a>1, and b+1 if b<L) are black stones, and also there are no other stones in between? Actually if there is a chain of white from a to b, to be captured, we need black stones at positions left and right such that all cells between them are white. That would mean left black stone is at a-1 (if a>1) and right black at b+1 (if b<L), and the interval (a-1, b+1) contains only white? But that's exactly the chain plus the black at ends. But what if there is an empty cell between chain and black stone? Then the chain is not directly adjacent; then it's not surrounded because the black stone is not adjacent; the capture condition requires the black stones to be at the boundaries of the interval, with the interior being white. If there is an empty between, then the interior includes an empty, so not all are white. So the chain is not captured. So a white chain is surrounded (i.e., would be captured) if and only if its left neighbor (if exists) is B and its right neighbor (if exists) is B. So we can use that as the condition for illegal move: after placement, there exists a white chain (maybe newly formed or extended) that has B on both sides.
But careful: If after placing a white stone, we might create a situation where a white chain is surrounded, but also at the same time that white chain might be part of a larger pattern? Actually it's simply: if any maximal contiguous segment of W's has both ends adjacent to B (or board edge? no, board edge not B), then it's illegal. So we must avoid creating any such white chain.
Now, also note that we might have a situation where a white chain becomes surrounded but also maybe that chain would be immediately captured? In Go, if you place a stone that creates a chain that is surrounded, that chain would be captured (and removed) immediately. But here they just say the player cannot make such a chain, so it's prohibited regardless of capture. So we follow that.
Now, the board initially has no surrounded chains. So there is no white chain with B on both sides initially, and no black chain with W on both sides initially.
Goal: For each empty cell p where we can legally place W, compute number of black stones that would be captured after that placement. Compute maximum over legal moves.
We need to simulate each move. Because L <= 100, we can do O(L^2) easily. But we should be careful about captures: when we place a white stone, we need to identify all black chains that become surrounded by white stones. Those black chains are captured. Note that the captured black stones are those that are between two white stones with no empties and no other colors? Actually condition: exist i and j such that S[i] = 'W', S[j] = 'W', and for all k in (i, j) S[k] = 'B'. So that captures all black stones in that interval. However, if there is a longer interval that contains both black stones and maybe empty? then not captured. So essentially, any maximal contiguous segment of B's that is flanked immediately by W's on both sides (i.e., left neighbor is W, right neighbor is W) becomes captured. But careful: what about if there are multiple white stones at ends? For a black segment from a to b (maximal B segment), if the cell immediately left of a (if a>1) is W and the cell immediately right of b (if b<L) is W, then that black segment is captured. That is exactly the definition. However, note that capture might also occur if the white stones are not directly adjacent? The definition says i-th and j-th grids contain white stones and every grid between i and j (i<k<j) contains a black stone. So the white stones are at the boundaries, and all cells between are black. So the black segment must be contiguous and must be exactly from i+1 to j-1. So if there is an empty or another color inside, it's not captured. So capturing requires that the black segment is completely enclosed by two white stones with no gaps. So for a maximal B segment, it's captured if its left neighbor is W and right neighbor is W. That is correct.
But there is a nuance: Could a black segment be captured even if it's not a maximal B segment? Actually if there are white stones at both ends but inside there are multiple B segments separated by empty? No because between i and j all must be B. So it must be a contiguous block of B. So indeed it's a maximal B segment.
Thus after placing a white stone, we need to examine all maximal B segments and count those that have W on both immediate sides.
But also, what about black stones that become captured due to the new white stone potentially connecting with existing white stones to form a longer white chain that then surrounds a black segment? Yes.
But also we need to consider that after capture, the captured stones are removed? The problem statement: "computes the maximum number of black stones which can be captured by the next move." It doesn't ask to update the board state after capture. We just need to count how many black stones would be captured. The capture event itself likely removes those stones, but for the purpose of counting the number captured, we don't need to simulate further moves. So we just count the black stones that become surrounded after the placement, regardless of whether other captures might happen as a result? Actually, the move is just placing a stone; then capture occurs: all chains that are surrounded are captured simultaneously. So we count those black stones.
But is there any scenario where capturing black stones could cause additional captures (like chain reactions)? Typically in Go, after placing a stone, any opponent chains that have no liberties are captured and removed. That might create new liberties for your own chains, but not cause further captures in the same move. But here, the capture rule is defined as "if i and j contain white stones and all between are black, these black stones will be captured". After they are captured and removed, there might now be white stones adjacent to other black stones, possibly making new captures? However, typical Go rules include that capturing is simultaneous and then after removal, no further captures are considered; but sometimes capturing one group may cause another to become captured (called snapback). In standard Go, you capture all groups with zero liberties at the same time. So if after removal, some groups become zero liberties, they are not captured because the move is already done? Actually in standard Go, after placing a stone, you first remove any opponent groups with zero liberties; then you check if your own group (the one you just placed) has any liberties; if not, that would be suicide, which is usually illegal in some rules, but some rules allow suicide if it captures opponent stones? But here they explicitly forbid suicide moves for white. So we only consider legal moves where no white chain becomes surrounded. So after placement, before any capture, there may be some white chains that are surrounded? But that would be illegal. So we only allow moves where there is no white chain with B on both sides. So after placement, we have some black segments that are surrounded by white. Those black segments are captured. The capture removal might cause white chains to become surrounded? Possibly if the black stones that were captured were providing a liberty to a white chain? Actually after capturing black stones, those cells become empty. This could create liberties for adjacent white chains. But the rule is: "if a chain is surrounded by opponent's stones, the chain will be captured." This happens based on the board state after the move but before capture? Or after capture? Typically, in Go, capture occurs based on the board after the stone is placed, but before any stones are removed. In this 1D version, they define capture as: if there is a configuration where a chain is flanked by opponent stones, then that chain is captured. I think it's based on the state after the move, not after removal. Because capture is defined simply on the state. So we can compute captured black stones directly from the state after placing the white stone (without removing anything). That's our count. However, what about white stones? They may be captured if surrounded? But that's illegal, so we don't consider such moves. So we can ignore capture of white stones for counting.
Thus algorithm: For each position p where S[p] == '.', do:
- Construct a new string T = S with T[p] = 'W'.
- Check if this board has any white chain surrounded by black. If yes, skip move (illegal).
- Else, count the number of black stones that are captured: i.e., count all indices where T[i] == 'B' and that belong to a maximal B segment whose left neighbor (i-1 if i>1) is 'W' or does not exist? Wait, for a B segment to be captured, it must be flanked by W on both sides. So for each maximal segment of consecutive B's from l to r, we need to check: (l == 1 ? left edge is board start, not W) or (T[l-1] == 'W')? Actually if l==1, left side is edge, not W, so not flanked. Similarly if r==L, right side edge. So captured if (l > 1 && T[l-1] == 'W') && (r < L && T[r+1] == 'W'). If both true, then all stones in that segment are captured; count (r - l + 1). Sum over all such segments.
- The count is the number of captured black stones for that move.
Now, we must be careful: Is it possible that a black segment is flanked by white stones that are not directly adjacent but with some empties? No, because for the capture condition, it says "every grid of index k (i < k < j) contains a black stone". So if there are empties, it's not captured. So our condition is correct.
Now, there is also nuance: Could a black segment be captured even if it is not maximal? Consider scenario: B B B W B? That doesn't make sense. Actually the capture condition captures contiguous black stones that are exactly between two white stones. If there is a longer segment with white on both sides but also some empties, not captured. So maximal B segments are the ones that could be captured.
Thus for each move we compute captured black stones accordingly.
Now, we must also ensure that the given initial state has no chains already surrounded. So there is no black chain flanked by W on both sides initially, and no white chain flanked by B on both sides. This simplifies.
Now, test examples:
Example 1: Input: 5 .WB.. -> S = ".WB.." (L=5). Let's compute.
Indices:1 '.';2 'W';3 'B';4 '.';5 '.'.
We need to find legal moves that capture max black stones.
Try placing at 1: T = "WWB.."? Actually if place at index1 -> "WWB.."? Wait original: S[1]='.' so place W at 1: T = "WWB.."? Actually after placing, T[1]='W', T[2]='W', T[3]='B', T[4]='.', T[5]='.'. Check white chains: there is a white chain from 1-2. Left neighbor? index0 none -> not surrounded (left edge). Right neighbor of chain is index3 'B'. So only one side B. So not surrounded. So legal. Now count captured black stones: There's a B at index3. Is it flanked by W on both sides? Left neighbor index2 is W, right neighbor index4 is '.' (empty). So not flanked. So captured =0.
Place at index4: T = ".WBW."? Actually S[4]='.', place W: T = ".WBW."? Wait: S[1]='.', S[2]='W', S[3]='B', S[4]='W', S[5]='.'. So T:1 '.',2 'W',3 'B',4 'W',5 '.'. White chains: chain at 2: single W, left neighbor index1 '.' not B; right neighbor index3 'B' -> not both B. Chain at 4: single W, left neighbor index3 'B', right neighbor index5 '.' -> not both B. So legal. Now capture black stones: B at index3: left neighbor index2 'W', right neighbor index4 'W' -> both W! So captured =1. That matches output 1. Place at index5: T = ".WB.W"? Actually S[5]='.' -> place W: T = ".WB.W"? Wait index5: ".WB.." -> index5 becomes W: ".WB.W"? But careful: S[1]='.',2='W',3='B',4='.',5='W' -> T: ".WB.W". White chains: chain at 2: left '.' right 'B' -> not both B; chain at5: left '.' (index4) is '.' -> not B. So legal. Captured black: B at 3: left W, right? index4 '.' -> not W. So 0. Also placement at index1 gave 0. So max=1.
Second example: Input: 5 .WBB. -> S = ".WBB." (L=5). Indices:1 '.';2 'W';3 'B';4 'B';5 '.'.
Check placements:
Place at 1: T = "WWBB."? Actually: 1='W',2='W',3='B',4='B',5='.'. White chain 1-2: left none, right index3 'B' -> not surrounded. So legal. Captured B: B segment 3-4: left neighbor index2 'W', right neighbor index5 '.' -> not both W =>0.
Place at 5: T = ".WBBW": white chain at 2: left '.' right 'B' -> not both B; chain at5: left 'B' (index4) right none -> not both B? Actually to be surrounded, both sides must be B. Here left is B but right is edge, not B. So legal. Captured B: B segment 3-4: left neighbor index2 'W', right neighbor index5 'W' -> both W => captured 2 stones. So count 2.
Place at 4? Actually index4 is 'B', not empty. Only empty are 1 and 5. So max 2 matches output.
Third example: Input: 6 .WB.B. -> S = ".WB.B." (L=6). indices:1 '.',2 'W',3 'B',4 '.',5 'B',6 '.'.
Placement possibilities: empty at 1,4,6.
Place at 1: T = "WWB.B."? Actually: 1='W',2='W',3='B',4='.',5='B',6='.'. White chains: 1-2: left none, right index3 'B' -> not surrounded. So legal. Captured B: B at 3: left index2 'W', right index4 '.' -> not both; B at5: left index4 '.' right index6 '.' -> not both. So 0.
Place at 4: T = ".WBWB."? Actually index4 becomes W: S:1 '.',2 'W',3 'B',4 'W',5 'B',6 '.'. White chains: chain at 2: left index1 '.' not B; right index3 'B' -> not both; chain at 4: left index3 'B', right index5 'B' -> left B, right B => white chain at index4 (single W) is surrounded! That would be illegal because it creates a surrounded white chain. So move illegal. (Mentioned in problem: cannot put white on 4th grid since chain of white stones will be surrounded by black stones). So skip.
Place at 6: T = ".WB.BW": index6='W': 1 '.',2 'W',3 'B',4 '.',5 'B',6 'W'. White chains: chain at2: left '.' right 'B' -> not both; chain at6: left 'B' (index5) right none -> not both. So legal. Captured B: B at3: left 'W' (index2), right index4 '.' -> not both; B at5: left index4 '.' right index6 'W' -> not both. So 0. So max 0 matches output.
Fourth example: Input: 6 .WB.WB -> S = ".WB.WB" (L=6) indices:1 '.',2 'W',3 'B',4 '.',5 'W',6 'B'. Empties: 1 and 4.
Place at 1: T = "WWB.WB": white chains: 1-2: left none, right index3 'B' -> not surrounded; chain at5: left index4 '.'? Actually index4 is '.'; right index6 'B' -> not both. So legal. Captured B: B at3: left index2 'W', right index4 '.' -> not both; B at6 is at end, left index5 'W', right none -> not both. So 0.
Place at 4: T = ".WBWWB"? Actually index4 becomes 'W': string:1 '.',2 'W',3 'B',4 'W',5 'W',6 'B'. Now white chains: chain from 2? Actually we have W at2, and W at4-5? But need to group contiguous: positions 2 is isolated? Let's see: index2='W', index3='B', index4='W', index5='W', index6='B'. So there are two separate white chains: chain at2: single W at index2, left index1 '.' not B, right index3 'B' => only right B; chain at4-5: indices 4-5 'W','W', left index3 'B', right index6 'B' => both sides B => white chain surrounded. So illegal. So max 0.
Fifth example: Input: 5 BBB.. -> S = "BBB.." (L=5). Empties: 4,5.
Place at 4: T = "BBB.W": indices:1 B,2 B,3 B,4 W,5 '.'? Actually if place at 4, original index4 '.' -> becomes W, index5 '.' remains. So string: "BBB.W"? Actually positions:1 B,2 B,3 B,4 W,5 . . Wait we need 5 length: "BBB.." -> after placing at index4: "BBB.W"? That would be indices:1 B,2 B,3 B,4 W,5 .? But we changed index4, index5 unchanged '.' => So T = "BBB.W"? Actually that is 1:B,2:B,3:B,4:W,5:. So not "BBB.W"? That's "BBB.W"? Actually the character for index5 is '.' so string is "BBB.W"? No, '.' is a dot, so it's "BBB.W"? That would be B B B . W? Wait careful: "BBB.." means indices:1 B,2 B,3 B,4 .,5 . . After placing at 4, we get B B B W . . Actually I think I mis-copied: index4 becomes W, index5 remains '.' => So T = "BBBW."? That is B B B W . . But that's 5 characters: positions:1 B,2 B,3 B,4 W,5 . . So string is "BBBW." (since dot at end). So check white chain: single W at index4, left neighbor index3 B, right neighbor index5 '.' => not both B, so legal. Captured black stones: B segment from 1-3, left neighbor? left of index1 none, right neighbor index4 W => not both W, so not captured. So 0.
Place at 5: T = "BBB.W"? Actually place W at index5: "BBB.W"? Actually original "BBB..": indices:1 B,2 B,3 B,4 .,5 . ; after placing at 5: B B B . W -> "BBB.W"? That's B B B . W. So T = "BBB.W"? Actually that's "BBB.W"? But index4 is '.'; index5 is W. So string: "BBB.W"? Write as "BBB.W" (dot at index4, W at index5). So white chain at index5: left neighbor index4 '.' not B; right none => legal. Captured black: B segment 1-3, left none, right neighbor index4 '.' => not both W. So 0. So max 0 matches output.
Thus algorithm seems correct.
Now, think about edge cases and subtlety: what about placing a white stone that connects two white chains? That could create a new white chain that may become surrounded? For example, if there are two separate white stones with black in between, and you fill an empty between them? That would connect them into a longer white chain, and the black stones that were between them become captured? Actually if you place a white stone in a gap between two white stones that have black stones in between, then after placement, the black stones might become surrounded and captured. But also the resulting white chain may be surrounded by black stones? Let's think: scenario: W . B B . W. If you place white on the empty between? Actually wait: The black stones between two white stones: that's exactly capture condition. But the initial state has no black chain surrounded because it says there are no chains already surrounded. So any such pattern would have at least one empty or something? Actually if we have W B B . W, the black chain from positions after W to before the empty: it's not fully surrounded because right side is empty, not W. So not captured. If we place white in the empty spot, we get W B B W W? Actually careful: Suppose S: "W B B . W" (positions 1 W,2 B,3 B,4 .,5 W). This has no captured black currently because between 1 and5, there is a '.' at 4, so not all black. If we place white at 4, we get "W B B W W". Now there are white stones at 1,4,5. The contiguous white chain? Actually positions 4-5 are a white chain, and position1 separate. But now the black segment 2-3: left neighbor index1 = W, right neighbor index4 = W => surrounded, so captured. Also check white chains: The white chain at index1 is single W, left none, right neighbor index2 B -> not both B. The white chain at 4-5: left neighbor index3 B, right neighbor index6? Actually L=5, so right neighbor of index5 is none, so not both B. So legal. So capture 2 black stones. That's fine.
Now, what about a move that creates a white chain that is surrounded by black stones? That's illegal. Example: . B . W . maybe? Let's find a case: If there is black stone on left of an empty and black on right, and you place white there: B . B -> placing W gives B W B. Then white chain single W is surrounded by B on both sides. That's illegal. Also if there are multiple whites: e.g., W B . B W? Actually if you have white on left, then black, then empty, then black, then white, placing white in empty would create? That would create ... Need to check: if we have pattern: W B . B W. Currently white at 1, white at5. If we place white at 3, we get W B W B W. Now what white chains? There is white at1 separate, white at3 separate, white at5 separate. Check chain at3: left neighbor index2 B, right neighbor index4 B => surrounded, illegal. So move illegal.
Also consider that after placing, the white chain might be formed by merging with adjacent white stones, and the merged chain could become surrounded. For example: pattern: B . W B. If you place white at the dot (index2) between B and W? Actually pattern: B . W B? That's indices:1 B,2 .,3 W,4 B. If place W at 2: B W W B. Then white chain from 2-3: left neighbor index1 B, right neighbor index4 B => surrounded, illegal. So illegal.
Thus condition for legality: after placement, for every maximal white segment [l,r], we must have either l==1 or T[l-1]!='B' or r==L or T[r+1]!='B' . Actually we need that it is NOT the case that (l>1 && T[l-1]=='B' && r<L && T[r+1]=='B'). So if any white segment satisfies both neighbors B, illegal.
Now, ensure to treat board edges as not B (or not existing).
Now, implementation: for each p where S[p]=='.', we can compute T as list of chars (maybe we just modify a copy). For each test we need to compute white segments and black segments. Complexity O(L^2) fine.
But careful: When we compute captured black stones, we must consider that after placement, the black stones captured are those with both sides W. However, what if the placement creates a white stone that makes two separate white chains become connected, and the black segment between them becomes captured. That's fine. However, we also need to consider that after capture, the board might change? But we only count captured stones based on the state after placement before any removal. That is what problem likely expects based on examples. Also the note about "the player can not make a chain of white stones which is surrounded by black stones even if it simultaneously makes some chains of black stones be surrounded." That implies that if a move would cause a white chain to be surrounded, it's illegal regardless of the capture of black stones. So they are only considering the state after placement, before capture, because the capture of black stones would happen simultaneously, but they say even if it captures black stones, still illegal. So the legality check is based on the state after placement, not after capture. So our approach stands.
Now, additional nuance: Could there be a scenario where placing a white stone results in a white chain that is surrounded, but also that white chain would be immediately captured? In Go, after placement, if a chain has no liberties, it is captured and removed. Then the board after removal might not have that white chain, so it wouldn't be considered illegal? But the statement says "the player can not make a chain of white stones which is surrounded by black stones even if it simultaneously makes some chains of black stones be surrounded." They emphasize "even if it simultaneously makes some chains of black stones be surrounded." This implies they are considering the state before capture. Because if after capture the white chain would be removed, then it wouldn't be there, but they still say can't make it. So they are prohibiting suicidal moves. So indeed legality check before capture.
Thus algorithm is straightforward.
Now, let's think about more complex patterns: Could a white placement cause more than one black segment to be captured? Yes, e.g., if there are multiple black segments flanked by white. We need to sum them.
Now, also note that after placement, a black segment might be flanked by white stones that are not directly adjacent to the segment if there are multiple white stones? Actually for a black segment from l to r to be captured, we need immediate neighbors: T[l-1]=='W' and T[r+1]=='W'. Because if there is a gap (empty or other color), not captured. So that's it.
Now, is there any scenario where a black stone could be captured because it's between a white stone and the edge? The problem explicitly said "The player cannot capture black stones by surrounding them with the edge of the game board and the white stone." So we must not count those. So our condition of both sides being W covers that.
Now, check initial condition: no chains already surrounded. So we don't have to worry about captures before move.
Thus algorithm is: For each empty index i:
- Build T = S with T[i] = 'W'.
- Check for any white chain that has black on both sides: iterate through indices to find white segments. For each segment from l to r (maximal consecutive W's), check if l>1 and T[l-1]=='B' and r<L and T[r+1]=='B'. If any such segment exists, move illegal, skip.
- Else, compute captured black stones: iterate through indices to find black segments. For each maximal B segment from l to r, check if l>1 and T[l-1]=='W' and r<L and T[r+1]=='W'. If true, add (r-l+1) to count.
- Keep max count.
Edge Cases:
-
L=1. Then board of length 1. S can be '.' or maybe B or W? But input guarantees at least one empty cell where player can put white stone and no surrounded chains. So if L=1, S must be '.' because only empty can place. Placing white at that cell yields board "W". Check legality: white chain from 1 to1, left and right edges none. So not surrounded (since both sides not B). So legal. Captured black: there are no black stones. So answer 0. That is fine.
-
L=2. possibilities: e.g., "..", place W at either. After placement, white chain: if we place at index1: "W." chain at1 left none, right '.' not B; okay. Also check captured black: none. If we place at both? Actually only one stone per move. So answer 0.
Now, any hidden traps: the legality condition may also need to consider that after placement, the white chain might be surrounded but if that white chain is adjacent to the edge? Not surrounded. So it's fine.
Now, test with more examples:
Consider: S = "B.WB." (L=5). Empties: positions 2? Actually indices:1 B,2 .,3 W,4 B,5 . . Place at 2: becomes B W W B . => T = "BWWB."? Actually index2='W', index3='W', index4='B', index5='.'. White chain from 2-3: left neighbor index1 'B', right neighbor index4 'B' => both B -> illegal. So cannot. Place at5: "B.WBW" -> T = "B.WBW"? Actually index5='W': 1 B,2 .,3 W,4 B,5 W. White chains: at3 single W: left index2 '.' not B; right index4 B -> only right B, not both. At5: left index4 B, right none -> not both. So legal. Captured black: B at1? left none, right index2 '.' not W; B at4: left index3 W, right index5 W => both W => captured 1. So answer 1.
Now, what about scenario where white placement creates a white chain that is surrounded but also captures black stones? Illegal. So not counted.
Now, the algorithm is simple.
But before finalizing, check if any other capture rule applies: maybe after placement, there could be capture of black stones even if they are not in a single segment flanked by immediate W? For example, pattern: W B . B W. If you place white in the dot, we get W B W B W. The black stones at indices2 and4 are not contiguous. Are they captured? According to capture definition: need i and j white such that all between are black. Here there are two separate black stones. After placement, we have white at1,3,5. Consider i=1, j=3: between is index2 which is B, so that black stone (index2) would be captured? The definition says "if i-th grid and j-th grids contain white stones and every grid of index k (i<k<j) contains a black stone, these black stones will be captured". So for i=1,j=3, indeed index2 is black, so captured. Similarly i=3,j=5: index4 is black, captured. So both black stones captured. In terms of maximal B segments: index2 is a B segment of length1, left neighbor index1 W, right neighbor index3 W => captured. Index4 is a B segment of length1, left neighbor index3 W, right neighbor index5 W => captured. So our method of checking maximal B segments works fine.
Now, what about a longer black segment with multiple white stones inside? Actually if you have W B B B W, the entire BBB is captured because it's flanked by W on both sides. That's fine.
Now, also consider scenario where there are white stones adjacent to each other, forming a chain, but also there might be black stones on both sides of that chain. That's not capture; that's white chain being surrounded, which is illegal if created. So no issue.
Thus algorithm seems correct.
But wait: Could there be a situation where a black segment is flanked by white stones that are not immediately adjacent because there are multiple white stones on one side? For example, pattern: W W B B W. Here the left side of the B segment is a white stone at index2? Actually indices: 1 W,2 W,3 B,4 B,5 W. The black segment from 3-4: left neighbor index2 is W, right neighbor index5 is W. So captured. That's immediate neighbors. Good.
Now, what about pattern: W . B B W? That's not captured because left neighbor of B segment is '.' not W.
Thus our condition is correct.
Now, about legality: "the player can not make a chain of white stones which is surrounded by black stones". So if after placement there exists a white chain that is flanked by black stones on both sides. This includes the case where the white chain is at the edge? No, because edge is not a black stone. So only interior.
Now, consider scenario: Placing white stone may create a white chain that is flanked by black stones but also maybe the black stones on both sides are part of black chains that would be captured? Even if they would be captured, the rule says "even if it simultaneously makes some chains of black stones be surrounded". So still illegal.
Thus we must check white chain condition before considering capture.
Now, note that the white chain could be created by merging with existing whites. So we must compute maximal white segments after placement.
Now, is there any other restriction? The problem says "It is guaranteed that the given state has at least one grid where the player can put a white stone and there are no chains which are already surrounded." So we don't need to consider moves that result in no legal moves.
Now, consider edge case: If a white stone placed creates a white chain that is surrounded, but also that white chain is exactly the stone placed, and it is between two black stones. This is illegal. So moves like placing in a gap between two black stones are illegal.
Now, implement.
Implementation details:
Read L and S. L may be up to 100, but S length L. We can ignore L (just use len(S)).
We'll create a function to compute captured count and legality for a given move position.
Pseudo:
max_captured = 0
for i in range(L):
if S[i] != '.':
continue
T = list(S)
T[i] = 'W'
# check legality: no white chain with B on both sides
legal = True
j = 0
while j < L:
if T[j] == 'W':
start = j
while j < L and T[j] == 'W':
j += 1
end = j - 1
# check neighbors
left_is_B = (start > 0 and T[start-1] == 'B')
right_is_B = (end < L-1 and T[end+1] == 'B')
if left_is_B and right_is_B:
legal = False
break
else:
j += 1
if not legal:
continue
# compute captured black stones
captured = 0
j = 0
while j < L:
if T[j] == 'B':
start = j
while j < L and T[j] == 'B':
j += 1
end = j - 1
left_is_W = (start > 0 and T[start-1] == 'W')
right_is_W = (end < L-1 and T[end+1] == 'W')
if left_is_W and right_is_W:
captured += (end - start + 1)
else:
j += 1
max_captured = max(max_captured, captured)
Print max_captured
Now, test with examples.
Example1: L=5, S=".WB..". Works.
Example2: .WBB. -> captured 2.
Example3: .WB.B. -> illegal move at index4, captured 0.
Example4: .WB.WB -> illegal move at index4, captured 0.
Example5: BBB.. -> captured 0.
All good.
But before final answer, consider a tricky case: Could there be a situation where a white chain is surrounded by black stones but the black stones are themselves part of a black chain that is flanked by white stones? For example, pattern: B . W B? Actually to have a white chain surrounded by black, there need to be black on both sides. Suppose we have B . W . B. If we place white at the empty between B and W? That creates B W W . B? That's B W W . B. The white chain from indices2-3: left neighbor index1 B, right neighbor index4 '.' not B -> not surrounded. So not illegal. To be surrounded, both sides must be black. So if we have B . . B, and we place white at the two empties? Actually single move only. So no.
Now, what about scenario: B W . W B. If we place white at the dot, we get B W W W B. White chain from indices2-4: left neighbor index1 B, right neighbor index5 B -> surrounded, illegal. That's clear.
Now, is there any scenario where a white chain becomes surrounded but it is not captured because the black stones on both sides would be captured? But the rule says illegal regardless, so we skip.
Thus algorithm stands.
Now, I will double-check the problem statement for any nuance about chain being "surrounded" and capture: They defined capture: "if i-th grid and j-th grids (1 < i+1 < j ≤ L) contain white stones and every grid of index k (i < k < j) contains a black stone, these black stones will be captured, and vice versa about color." Note the condition 1 < i+1 < j ≤ L, meaning i and j must be at least 1 and L, but they wrote (1 < i+1 < j ≤ L) which simplifies to i ≥ 1 and j > i+1. So they require at least one black stone between? Actually if i+1 < j, then there is at least one index between. So capture requires at least one black stone between the two white stones. That's fine.
Now, the definition of chain being surrounded: They said: "When a chain is surrounded by stones with opponent's color, the chain will be captured." So that's the same. So for a white chain to be surrounded, it would be between two black stones (with at least one white stone inside). So that matches our condition.
Now, the restriction: "the player can not make a chain of white stones which is surrounded by black stones even if it simultaneously makes some chains of black stones be surrounded." So we must check that.
Now, note that the initial state has no chains already surrounded, so no immediate capture.
Now, there is also a note: "Some of the intuition obtained from the original Go may curse cause some mistakes." So they want us to follow these rules exactly.
Now, after we have algorithm, we can implement.
Time complexity O(L^2) = 10000, fine.
But we need to be careful about reading input: The input format is "L S". There is a space between L and S. So we read L and then S. L may be up to 100.
Now, implement in Python.
But before finalizing, I want to test some custom cases to be absolutely sure.
Case 1: L=3, S="B.B". Empties: index2 '.' between two B's. Placing W at index2 yields B W B. White chain at index2: left B, right B -> surrounded, illegal. So no legal moves? But problem guarantees at least one legal move. So such initial state wouldn't appear because then there would be no legal move (since only empty is illegal). Indeed they guarantee at least one grid where player can put a white stone (legal). So initial state won't be like that.
Case 2: L=3, S=".B." Empties: index1 and3. Place at1: W B . -> white chain at1 left none, right B => not both, legal. Capture? Black at index2: left neighbor index1 W, right neighbor index3 '.' => not both W, so 0. Place at3: . B W -> similar, 0. So answer 0.
Case 3: L=4, S="B..B". Empties: 2,3. Place at2: B W . B -> white chain at2: left B, right '.' => not both B? Actually right is '.', not B, so legal. Capture? B at1: left none, right index2 W; B at4: left index3 '.'; not captured. Place at3: B . W B -> white chain at3: left '.'? Actually index2 '.'; right B; not both B. Legal. Capture? B at1 left none; B at4 left index3 W; right none; no capture. So 0.
Case 4: L=5, S="W.B.W". Empties: index3? Actually indices:1 W,2 .,3 B,4 .,5 W. Empties:2 and4. Place at2: W W B . W -> white chain at1-2: left none, right index3 B -> not both B, legal. Captured: B at3: left index2 W, right index4 '.' => not both. 0. Place at4: W . B W W -> white chain at5? Actually indices:1 W,2 .,3 B,4 W,5 W. White chains: at1: left none, right index2 '.'; chain at4-5: left index3 B, right none => not both. Legal. Captured: B at3: left index2 '.'? Actually index2 is '.', not W, right index4 W, so not both. So 0. But is there capture by connecting? If we place at2 we get white at1,2 and white at5, but B at3 not flanked because right side is empty. If we place at4, B at3 left is '.' not W. So 0.
Case 5: L=6, S="W..B.W". Empties:2,3,5? Actually indices:1 W,2 .,3 .,4 B,5 .,6 W. Empties:2,3,5. Place at5: W..B W W? Actually if place at5: indices:1 W,2 .,3 .,4 B,5 W,6 W -> string "W..BWW". White chains: at1: left none, right index2 '.'; at5-6: left index4 B, right none. Not surrounded. Capture? B at4: left index3 '.'? Actually index3 is '.'; right index5 W -> not both. So 0. Place at2: "WW.B.W" -> indices:1 W,2 W,3 .,4 B,5 .,6 W. White chain at1-2: left none, right index3 '.'; at6: left index5 '.'; legal. Capture? B at4: left index3 '.' not W; right index5 '.' not W => 0. Place at3: "W.WB.W"? Actually if place at3: 1 W,2 .,3 W,4 B,5 .,6 W -> "W.WB.W". White chains: at1: left none, right index2 '.'; at3: left index2 '.'; right index4 B; at6: left index5 '.'; legal. Capture? B at4: left index3 W, right index5 '.' => not both. So 0. So answer 0.
Case 6: L=7, S="B..W..B". Empties:2,3,5,6. Some moves might capture? Place at5? Actually we need pattern where B's become flanked by W after placement. For example, if we have W at left and right of a B segment. Currently leftmost B at index1, rightmost B at index7. There is a W at index4. So if we place white at index2? Then we get B W . W .. B? Actually index2 becomes W: string: B W . W . . B. That yields B at1, and B at7. No B segment flanked because between white stones? Let's systematically try to create capture: We want to have a B segment with W on both sides. Possibly by placing white such that the existing W at 4 and new W enclose a B segment. For example, if we place white at index6: B..W.WB? Actually indices:1 B,2 .,3 .,4 W,5 .,6 W,7 B -> string "B..W.WB"? Actually index7 B, so after placing at6: B..W.WB -> that is B . . W . W B? Wait order:1 B,2 .,3 .,4 W,5 .,6 W,7 B. So we have W at4 and W at6, with index5 '.' in between? That's not B. So no. Place at3: B . W W . . B -> B . W W . . B. Then we have W at3-4? Actually contiguous W at 3-4? That's one white chain. B segment at1? Not flanked; B at7? Not flanked. Maybe there is no B between W's. So capture 0.
Now, try a pattern: S = "WBB..W". Empties:4,5? Actually indices:1 W,2 B,3 B,4 .,5 .,6 W. Place at4: W B B W . W -> "WBBW.W". White chains: at1: left none, right B; at4: left B, right '.'; at6: left '.'? Actually index5 '.'? Wait after placement:1 W,2 B,3 B,4 W,5 .,6 W -> string "WBBW.W". White chain at1: left none, right B (index2) -> not both; chain at4: left B (index3), right index5 '.'; chain at6: left index5 '.'; so legal. Captured black: B segment at2-3: left neighbor index1 W, right neighbor index4 W -> both W => captured 2. So max 2. That's a legal move. Check if white chain at4 is surrounded? left B, right '.' -> not both B, ok. So legal. Good.
Now, test legality on a similar pattern but where the new white is adjacent to another white that creates a chain surrounded. E.g., "WBBW."? Actually L=5, S="WBBW." -> indices:1 W,2 B,3 B,4 W,5 . . Place at5: W B B W W -> "WBBWW". White chains: at1: left none, right B; at4-5: chain from4-5: left neighbor index3 B, right none -> not both B, so legal. Captured: B segment 2-3: left W, right W (index4) -> captured 2. So legal. That's fine.
Now, if we have "BWWB."? Actually S="BWWB." -> indices:1 B,2 W,3 W,4 B,5 . . Place at5: B W W B W -> "BWWBW". White chains: at2-3: left neighbor index1 B, right neighbor index4 B -> both B => illegal! So move illegal. So that pattern is illegal because the white chain becomes surrounded by black. So indeed, need to avoid moves that create surrounded white chain.
Now, what about a move that creates a white chain that is surrounded, but the black stones that surround it are themselves part of black chains that would be captured? Example: B W . B. Wait we need a white chain surrounded by black, but those black stones might be flanked by white after the move? For instance, pattern: B . W B . B? Hmm.
Consider S = "B.WB" L=4? Actually indices:1 B,2 .,3 W,4 B. Place at2: B W W B -> "BWWB". White chain from2-3: left B, right B => surrounded, illegal. Also, black stones at1 and4: black at1 left none, right index2 W; black at4 left index3 W, right none; not flanked. So illegal.
Now, consider S = "W B . B W"? Actually L=5, S="WB.BW"? indices:1 W,2 B,3 .,4 B,5 W. Place at3: W B W B W -> "WBWBW". White chains: at1: left none, right B; at3: left B, right B => surrounded, illegal. However, note that black stones at2 and4 are each flanked by W? Actually black at2: left W (index1), right W (index3) => captured. black at4: left W (index3), right W (index5) => captured. So it would capture 2 black stones, but move is illegal because white chain at3 is surrounded. So such a move is not allowed. Good.
Now, does our legality check catch this? Yes, white chain at index3 (single) has left B and right B, so illegal.
Now, is there any scenario where after placement, there exists a white chain that is surrounded but also that white chain is not the one we placed? Possibly if placing a white stone merges two white chains that were separated by a black stone that is captured? Wait, but we are not removing stones before legality check. So if there was an existing white chain that was not surrounded before, after placing a stone, it could become surrounded? For example, consider board: W . B B W. Initially white at 1 and 5, black at 3-4. If we place white at 2, we get W W B B W. The white chain from 1-2 is not surrounded (left none, right B). The white chain at 5 is not surrounded (left B, right none). So ok. If we place white at 3? That's B, not empty. So not.
Consider board: B W . W B. Initially white at2 and4, black at1 and5. If we place white at3: B W W W B. White chain from2-4: left B (index1), right B (index5) => surrounded, illegal. So the chain becomes surrounded.
Now, what about board: W B . B W . B? Something like that. But seems fine.
Now, one more nuance: Could there be a white chain that becomes surrounded because the new white stone causes the left or right neighbor to become black? No, stones don't change color. So only the adjacency changes because the white stone extends or creates a chain, which may cause its neighbors to be black on both sides.
Thus algorithm stands.
Now, consider that the board may have multiple white chains. When we place a white stone, it can either: become a new isolated white stone (if both adjacent cells are not white), or it can merge with one or two existing white chains (if left neighbor is white, right neighbor is white, merging them into one chain). So we need to correctly handle the merging case for legality check: because the resulting chain might be surrounded. So our method of scanning for maximal white segments after placement automatically handles merging.
Now, confirm that the initial board has no surrounded chains. So we don't have to worry about capturing black before move.
Now, think about a more complex case: after placement, multiple black segments may be captured, and also, could there be overlapping captures? For example, consider pattern: W B . B W, placing white at dot yields W B W B W. Two separate black segments are captured. That's fine.
Now, what about pattern: W B B . B W? Actually L=6: indices:1 W,2 B,3 B,4 .,5 B,6 W. Place at4: W B B W B W. Now, we have white at1,4,6. B segment at2-3: left W (1), right W (4) -> captured 2. B segment at5: left W (4), right W (6) -> captured 1. So total 3. That's fine.
Now, legality: white chain at1: left none, right B; chain at4: left B, right B? Actually index4 is W, left neighbor index3 B, right neighbor index5 B -> That's surrounded! So move illegal. So indeed, placing at4 creates a surrounded white chain at4, so illegal. So that move is not allowed even though it would capture many black stones. So only legal moves are those that don't create a surrounded white chain.
Now, what about placing at some other position? In that pattern, empties only at4? Actually also maybe other empties? Only index4 is empty. So no legal moves? But problem guarantees at least one legal move, so such pattern wouldn't appear.
Now, consider pattern: W B . . B W. empties:3,4. Place at3: W B W . B W -> "WBW.BW". White chains: at1: left none, right B? Actually index1 W, index2 B -> chain at1 isolated, left none right B; at3: left B, right index4 '.'; at6: left B? Actually index5 B, index6 W -> chain at6: left B, right none. No white chain surrounded. So legal. Capture? B segment at2: left W(1), right W(3) -> captured 1. B segment at5: left index4 '.'? Actually index5 B, left index4 '.' not W, right index6 W => not captured. So total 1. Place at4: W B . W B W -> "WB.WBW". White chains: at1: left none, right B; at4: left index3 '.'? Actually index3 is '.'? Wait after placement: indices:1 W,2 B,3 .,4 W,5 B,6 W. White at1,4,6. Chain at4: left index3 '.'; chain at6: left index5 B; not both B. So legal. Capture? B at2: left W(1), right index3 '.' -> not; B at5: left W(4), right W(6) -> captured 1. So total 1. So answer 1.
All good.
Now, let's consider board with more than one white chain and check legality of merge.
Example: S = "W..W". L=4. Empties:2,3. Place at2: W W . W -> "WW.W". White chains: indices1-2 chain, index4 chain. Chain1-2: left none, right index3 '.' -> not surrounded. Chain4: left index3 '.' , right none -> not. So legal. Place at3: W . W W -> "W.WW". Chain at1: left none, right '.'; chain at3-4: left index2 '.', right none -> legal. So ok.
Now, what about S = "W.BW"? Actually L=4: indices:1 W,2 .,3 B,4 W. Empties:2. Place at2: W W B W -> "WWBW". White chains: at1-2: left none, right B; at4: left B, right none; not surrounded. Legal. Captured? B at3: left W(2), right W(4) -> captured 1. So that's legal and captures.
Now, S = "W.B.BW"? L=6:1 W,2 .,3 B,4 .,5 B,6 W. Empties:2,4. Place at2: W W B . B W -> "WWB.BW". White chains: 1-2 left none, right B; 6 left B; legal. Captured? B at3: left W(2), right index4 '.' -> no; B at5: left index4 '.' not W -> no; so 0. Place at4: W . B W B W -> "W.BWBW". White chains: 1 left none, right '.'; chain at4: left B, right B; Actually index4 W, left index3 B, right index5 B => white chain at4 is surrounded! So illegal. So move illegal.
Now, we see pattern.
Thus algorithm seems correct.
Now, I will also consider if there is any scenario where a white chain becomes surrounded but we might have missed it due to not considering board edges? No, because edges are not black.
Now, I'd like to ensure that the legality condition should be applied to all white chains, including the ones that are at the edge? For example, if board is "BW" and we place at an empty? Not relevant.
Now, finalize.
But wait: Could there be a scenario where a white stone is placed and creates a white chain that is surrounded by black stones, but that white chain is not the one we placed? For example, consider initial: B W . B. Initially white chain at2 is not surrounded because left B, right '.'? Actually it's B W . B -> white at2, left B, right '.' => not surrounded. Now, if we place white at3, we get B W W B. Then white chain from2-3 is surrounded by B on both sides? left B (index1), right B (index4) -> yes, illegal. That's the chain that includes the placed stone. But can we create a surrounded white chain that does not include the placed stone? Suppose we have two separate white chains, and placing a stone between them merges them into one chain that might be surrounded. That chain includes the placed stone. So always includes placed stone? Possibly not: Could an existing white chain become surrounded because the new white stone changes its neighbor? For example, consider board: W B . B .? Actually if we have a white chain, its neighbors are fixed unless we place a stone adjacent to it, which changes the neighbor? Actually placing a white stone adjacent to a white chain will merge with it, so the resulting chain includes the new stone. So any change to the neighbors of an existing white chain involves placing a stone adjacent to it, which merges. So the chain after placement will include the new stone if it is adjacent; otherwise, if you place far away, it won't affect that chain's neighbors. So the only way an existing white chain could become surrounded without merging with the new stone is if the new stone changes the cell that is a neighbor of the white chain from something else to a black stone? But stones don't change color, and you only place a white stone. So you cannot change a cell from something to black. The only changes are: a '.' becomes 'W'. So if an existing white chain had a neighbor that was '.' and you place a white stone somewhere else, that neighbor remains '.' unless you place exactly there. So an existing white chain could become surrounded if its neighbor was something else? Not possible. So the only white chains that can become surrounded are those that include the newly placed white stone (since that's the only change). So we could check just the chain containing the placed stone. However, it's safer to check all white chains, but it's okay.
Now, another nuance: The problem says "the player can not make a chain of white stones which is surrounded by black stones even if it simultaneously makes some chains of black stones be surrounded." This indicates that the move is illegal if, after placement, there exists a white chain that is surrounded. So we need to check all.
Now, we have algorithm. Let's test some random cases via mental simulation and also perhaps with a quick program later, but since it's an explanation, we should be confident.
Now, check with more complicated case: L=8, S=".B.BW.B." (just random). We'll not test all.
Now, consider something about capturing multiple black segments that are adjacent? Actually they'd be separate.
Now, I think we're ready.
But before finalizing, let's examine the definition again: "if i-th grid and j-th grids (1 < i+1 < j ≤ L) contain white stones and every grid of index k (i < k < j) contains a black stone, these black stones will be captured, and vice versa about color." This definition might imply that only the black stones strictly between the two white stones are captured, not necessarily all black stones that are flanked by white stones? But if there is a white stone at i and another at j, and all between are black, then those black stones are captured. However, if there are multiple white stones, could there be overlapping intervals? For example, pattern: W B W B W. The definition suggests that for i=1, j=3, the black at 2 is captured. For i=3, j=5, the black at 4 is captured. So each black stone is captured because there exists a pair of white stones that enclose it. That matches our segment approach: each maximal B segment that is flanked by W on both sides is captured entirely? But consider pattern: W B B W B W. This is indices:1 W,2 B,3 B,4 W,5 B,6 W. Our segment method: B segment at2-3: left W(1), right W(4) -> captured 2. B segment at5: left W(4), right W(6) -> captured 1. So total 3. According to definition, for i=1, j=4: captures 2-3. For i=4, j=6: captures 5. So yes.
Now, what about pattern: W B W B B W? indices:1 W,2 B,3 W,4 B,5 B,6 W. Here there is B at2 between W1 and W3 -> captured? But between W1 and W3, all indices 2? Actually indices 2 is B, so captured. Then B segment at4-5: left neighbor index3 W, right neighbor index6 W -> captured 2. So total 3. Our method: B segment at2: left W1, right W3 -> captured 1. B segment at4-5: left W3, right W6 -> captured 2. That's 3.
Now, what about pattern: W B W W B W? indices:1 W,2 B,3 W,4 W,5 B,6 W. B at2: left W1, right W3? Actually index3 is W, so flanked, captured. B at5: left index4 W, right index6 W -> captured. So 2.
All consistent.
Now, consider a scenario where a black segment is flanked by white but there is also another white inside the segment? Not possible because segment is contiguous black.
Now, any other nuance: Could a black stone be captured if it is between a white stone and an edge? No, because edge is not a white stone.
Thus algorithm fine.
Now, we need to implement in Python and maybe handle input reading with sys.stdin.
Now, I'd like to ensure that the code is efficient and correct.
One more test: L=100 all '.'? Then any placement is legal? Check: placing a white stone anywhere would create a white chain of length1 with neighbors '.' or edge, not both B, so legal. Capture count =0 because no B. So answer 0.
Now, L=100, all 'B'? But there must be at least one '.' to place, and also no surrounded chains initially. If all B, there are no '.' so invalid by guarantee. So not possible.
Now, S might have both B and W, and the only legal moves are those that don't create surrounded white. Since initial has no surrounded chains, there is at least one such move.
Now, let's try to find a case where the maximum captured is not trivial. It's fine.
Now, perhaps there is a nuance about the rule "the player can not make a chain of white stones which is surrounded by black stones even if it simultaneously makes some chains of black stones be surrounded." This might also mean that after placement, if any white chain becomes surrounded, the move is illegal, regardless of whether that chain would be immediately captured. So we do that.
Now, the problem statement says "It is guaranteed that the given state has at least one grid where the player can put a white stone and there are no chains which are already surrounded." So initial state is clean.
Now, let's consider if after placement, a white chain could be surrounded but also that chain might be adjacent to an empty that becomes W? No.
Thus answer is computed as described.
Now, I'll write the code.
But wait, we need to think about a scenario where placing a white stone causes a black chain to be captured, and that capture might remove the black stones that were surrounding a white chain, thereby preventing that white chain from being surrounded? However, as argued, capture is based on state before removal. So if the white chain is surrounded by black stones in that state, it's illegal, even if those black stones would be captured later. So we cannot salvage such a move. So we must check the state before capture. That's what we do.
Now, everything is good.
Now, let's produce final answer.
But before that, I'd like to do a thorough reasoning about the correctness of the algorithm in all cases.
We can consider the board after placement as a string. Let's define:
-
A white chain is a maximal consecutive sequence of 'W'.
-
It is "surrounded" if both ends have adjacent cells (if exist) that are 'B'. That means if the chain does not touch an edge, and the cells immediately before and after are 'B'. (Since the board is 1D, adjacency is direct neighbor.)
-
A black chain is captured if it is flanked by 'W' on both sides (i.e., does not touch an edge, and neighbors are 'W').
These definitions are consistent with the problem's capture description.
Now, our algorithm checks these conditions exactly.
Now, the only potential pitfall: Could there be a situation where a white chain is considered surrounded even if it's at the edge but the other side is B? That would still require both sides B, but edge is not B, so no.
Now, is there any scenario where a white chain that is not maximal? Actually we consider maximal.
Now, might there be a scenario where a white chain is surrounded by black stones but not captured because there is an empty cell between the white chain and the black stone? The definition of surrounded seems to require that the chain is directly adjacent to opponent stones. Because the capture definition says "if i-th grid and j-th grids contain white stones and every grid of index k (i < k < j) contains a black stone, these black stones will be captured." For a white chain to be captured, we need black stones at i and j and all between are white. That would imply that the white chain is directly between two black stones, with no empties. So our condition of having B on both immediate sides is correct.
Thus algorithm is correct.
Now, think about the input parsing: The problem statement has "Input consists of one line: L S". So we can read with input().split().
Now, also note that there might be spaces or newline after? Not.
Now, produce final code.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().split()
if not data: return
L = int(data[0])
S = data[1].strip()
# Maybe length check, but assume correct.
max_cap = 0
n = L
for i in range(n):
if S[i] != '.':
continue
# build T as list
T = list(S)
T[i] = 'W'
# check white chains for surrounded
illegal = False
j = 0
while j < n:
if T[j] == 'W':
start = j
while j < n and T[j] == 'W':
j += 1
end = j - 1
left_black = (start > 0 and T[start-1] == 'B')
right_black = (end < n-1 and T[end+1] == 'B')
if left_black and right_black:
illegal = True
break
else:
j += 1
if illegal:
continue
# count captured black stones
captured = 0
j = 0
while j < n:
if T[j] == 'B':
start = j
while j < n and T[j] == 'B':
j += 1
end = j - 1
left_white = (start > 0 and T[start-1] == 'W')
right_white = (end < n-1 and T[end+1] == 'W')
if left_white and right_white:
captured += (end - start + 1)
else:
j += 1
max_cap = max(max_cap, captured)
print(max_cap)
if name == "main":
solve()
Now, test with given examples.
Example 1: L=5, S=".WB..". Let's manually run:
Loop i=0 ('.'): T = "WWB.."? Actually i=0 (index0) => T[0]='W': string "WWB..". White segments: indices0-1 -> start=0, end=1, left_black? start>0 false, so not surrounded. So legal. Black segments: indices2 'B' -> start=2 end=2, left_white? index1 is W => true, right_white? index3 '.' => false, so not captured. captured=0. max=0.
i=1 is 'W' skip.
i=2 is 'B' skip.
i=3 '.' -> T = ".WBW."? Actually index3 becomes W: ".WBW." (positions:0 '.',1 W,2 B,3 W,4 '.'). White segments: at index1: start=1 end=1, left_black? index0 '.' not B, right_black? index2 B -> only one; at index3: start=3 end=3, left_black? index2 B, right_black? index4 '.' -> only one; not surrounded. Legal. Black segment at index2: left_white index1 W, right_white index3 W => captured=1. max=1.
i=4 '.' -> T = ".WB.W"? Actually index4 becomes W: ".WB.W" (0 '.',1 W,2 B,3 '.',4 W) -> white segments: index1: left '.' right B; index4: left '.' right none? Actually index4 is last, right none, so not surrounded. Legal. Black segment at2: left W, right index3 '.' -> not captured. So captured 0. max remains 1. Output 1.
Matches.
Example2: L=5, S=".WBB." -> i=0 '.' gives "WWBB." captured 0. i=4 '.' gives ".WBBW" captured 2. max 2.
Example3: L=6, S=".WB.B." -> i=0 '.' -> "WWB.B." captured 0. i=3 '.'? Actually index3 is '.'? Wait string ".WB.B." indices:0 '.',1 'W',2 'B',3 '.',4 'B',5 '.'. So i=3 '.' -> T becomes ".WBWB."? Actually index3 becomes W: string ".WBWB."? Let's compute: 0 '.',1 W,2 B,3 W,4 B,5 '.'. White segments: at1: left '.' right B; at3: left B right B? Actually index3 W, left index2 B, right index4 B -> both B => surrounded => illegal. So skip. i=5 '.' -> T = ".WB.BW"? Actually index5 becomes W: ".WB.BW" -> check white segments: at1: left '.' right B; at5: left B right none; legal. Black segments: at2: left W, right index3 '.'; at4: left index3 '.' right W -> none captured. So max 0.
Example4: L=6, S=".WB.WB" -> i=0 '.' -> "WWB.WB"? Actually index0 becomes W: "WWB.WB" -> legal? White segments: indices0-1 chain: left none, right B -> not both; index4? Actually after placement: positions:0 W,1 W,2 B,3 '.',4 W,5 B? Wait original ".WB.WB": 0 '.',1 W,2 B,3 '.',4 W,5 B. So after i=0: 0 W,1 W,2 B,3 '.',4 W,5 B. White chains: 0-1 left none right B; 4 left '.' right B. Not surrounded. Legal. Captured: B at2 left W, right '.'; B at5 left W? Actually index5 B left index4 W, right none. So not captured. captured 0. i=3 '.' -> T = ".WBWWB"? Actually index3 becomes W: 0 '.',1 W,2 B,3 W,4 W,5 B. White chains: at1: left '.' right B; at3-4: left B (index2) right B (index5) => both B -> illegal. So skip. max 0.
Example5: L=5, S="BBB.." -> i=3 '.'? Actually indices:0 B,1 B,2 B,3 .,4 . So i=3 '.' -> T = "BBBW."? Actually index3 becomes W: "BBBW." -> white chain at index3: left B, right '.' -> not both; legal. Black segment 0-2: left none, right W -> not captured. captured 0. i=4 '.' -> T = "BBB.W" -> white chain at4: left '.'? Actually index3 is '.'; so left '.' right none; legal. captured 0. max 0.
All good.
Now, we must ensure that we treat indices correctly: using 0-index.
Now, finalize.
But we need to consider that the board length L can be up to 100, but S may have spaces? No.
Now, we can produce final answer.
One more check: The problem's example table shows Input| Output but in the problem statement they have Input| Output with the example lines. In the given text, it's a bit messy but we have them.
Now, we can provide the code.
Now, double-check: The input lines in the problem statement are like:
5 .WB..
But in the example table, they show Input| Output and then some lines. In the text it's:
5 .WB..
|
1
5 .WBB.
|
2
6 .WB.B.
|
0
6 .WB.WB
|
0
5 BBB..
|
0
So they have some extra vertical bars and spaces. But actual input would be one line with L and S. So our reading method works.
Now, I think it's correct.
Now, maybe there is a more efficient way, but not needed.
Now, I'll produce final answer.
But wait: I want to reflect on possible misinterpretation: The definition of capture says "if i-th grid and j-th grids (1 < i+1 < j ≤ L) contain white stones and every grid of index k (i < k < j) contains a black stone, these black stones will be captured, and vice versa about color." This might be interpreted that only the black stones that are exactly between two white stones with no other stones in between are captured. That's what we used. However, could there be a case where there are multiple white stones within the interval? For example, if we have white stones at i and j, but also a white stone at some k in between? Then the condition "every grid of index k (i < k < j) contains a black stone" would be false because there is a white stone. So that black segment would not be captured. So that's consistent.
Now, what about pattern: W B W B W? The black stones at positions 2 and 4 are captured, as we said. That's fine.
Now, think about if there is a black chain that is flanked by white stones but also there is an empty cell adjacent to it? Not captured.
Thus all good.
Now, final code.
But the problem statement says "Write a program that computes the maximum number of black stones which can be captured by the next move of the white stones player." So output just integer.
Now, produce final answer.
One more nuance: In the legality check, we must ensure that we are not allowing moves that create a white chain that is surrounded, but also that white chain may be of length >1. Our condition left_black and right_black applies regardless of length. So fine.
Now, might there be a scenario where a white chain is surrounded by black stones but also there is an empty cell between the chain and a black stone? That wouldn't be surrounded because the immediate neighbor is not black. So not an issue.
Now, let's test a more complex scenario where the placed stone merges two white chains and the resulting chain may have black on both sides, but perhaps one of the sides is not black because the black stone is actually part of a black chain that is captured? But again, before capture, it's still black. So it counts. So illegal.
Thus fine.
Now, I'm confident.
Now, write final answer with explanation and code.
But as ChatGPT, I need to produce final answer: Provide code in Python.
I'll also add comments.
Now, finalize. The problem can be solved by simulating each possible placement of a white stone on an empty cell, checking if the move is legal (does not create a surrounded white chain), and counting the black stones that become captured. Since the board length is at most 100, a simple O(L²) brute-force approach is sufficient.
Algorithm
- Read the board length
L and the state string S.
- For each position
i where S[i] == '.':
- Create a copy
T of S with T[i] = 'W'.
- Legality check: Scan
T to find all maximal contiguous white chains.
A white chain is surrounded (and thus the move is illegal) if it has a black stone immediately to its left and a black stone immediately to its right. If any such chain exists, skip this move.
- Capture count: Scan
T to find all maximal contiguous black chains.
A black chain is captured if it has a white stone immediately to its left and a white stone immediately to its right. Add its length to the total captured stones for this move.
- Keep the maximum capture count over all legal moves and output it.
Implementation in Python
import sys
def solve() -> None:
data = sys.stdin.read().split()
if not data:
return
L = int(data[0])
S = data[1].strip()
n = L
max_captured = 0
for i in range(n):
if S[i] != '.':
continue
# simulate placing a white stone at position i
board = list(S)
board[i] = 'W'
# ---- legality check: no surrounded white chain ----
illegal = False
j = 0
while j < n:
if board[j] == 'W':
start = j
while j < n and board[j] == 'W':
j += 1
end = j - 1
left_black = (start > 0 and board[start - 1] == 'B')
right_black = (end < n - 1 and board[end + 1] == 'B')
if left_black and right_black:
illegal = True
break
else:
j += 1
if illegal:
continue
# ---- count captured black stones ----
captured = 0
j = 0
while j < n:
if board[j] == 'B':
start = j
while j < n and board[j] == 'B':
j += 1
end = j - 1
left_white = (start > 0 and board[start - 1] == 'W')
right_white = (end < n - 1 and board[end + 1] == 'W')
if left_white and right_white:
captured += end - start + 1
else:
j += 1
max_captured = max(max_captured, captured)
print(max_captured)
if __name__ == "__main__":
solve()