We need to simulate process of repeatedly deleting points that have at least one neighbor of a different color. Each operation deletes all points satisfying condition simultaneously (based on original configuration at the start of the operation). After deletions, points become adjacent with new neighbors, and we repeat.
Goal: count number of operations performed until no deletions possible (i.e., all remaining points have same color as both neighbors? Actually condition: a point is deleted if it has a neighbor point of a different color than itself. So if a point has at least one neighbor of a different color, it gets deleted. So after process stops, the remaining set must have every point's both neighbors (if exist) same color as itself; i.e., all remaining points same color (since neighbors must be same color, transitivity gives all same color). Also isolated points (only one point) have no neighbors, so they are safe? Check condition: "Each point has at most two neighbors - one from the left and one from the right." Neighbors are points which don't have any other points between them. So a point with no neighbors (i.e., alone) does not have any neighbor of different color, so it is not deleted. So final state can be a single point or multiple points all same color (since if multiple same color, each neighbor is same color, condition fails). So the process stops when the remaining string is all same letter (or empty? Actually after deletions we may have empty set? If all points deleted, then no operation can be performed, but number of operations performed is whatever ended with empty set. The question: "How many operations will you need to perform until the next operation does not have any points to delete?" So if after k operations, either the set is empty or all points have same color (so no further deletions). So answer is k.
We need to compute number of operations for given string of length n up to 1e6.
This is reminiscent of "string reduction" like "remove characters that have adjacent different characters" similar to "carnival" or "deletion distance". This is essentially the process of repeatedly removing all characters that are adjacent to a different character (border characters? Actually any character that has a neighbor with different color). Equivalent to: each operation removes all characters that are not in a "monochromatic block" that survives? Not exactly.
We need to simulate efficiently. Observations: At each step, only characters on boundaries of color blocks are vulnerable? Let's think: Initially, we have runs of same letters. Consider a run of length L of same letter, with left neighbor letter (if any) and right neighbor letter (if any) possibly different. Points inside the run: interior points have both neighbors same color (since within run), so they are NOT deleted. Edge points: the leftmost point of the run has a left neighbor (if run is not at leftmost) of possibly different color; similarly rightmost point has right neighbor possibly different. If either neighbor is different, that edge point will be deleted. If the run is at the end, the leftmost point (if left end) has no left neighbor, so it's safe. However, careful: if left neighbor is same color? That would mean the run is merged with neighbor, but by definition runs are maximal, so neighbors are different (or nonexistent). So edge points have neighbor of different color, thus they get deleted. So in first operation, all points that are not at the very ends of the string? Wait: For the leftmost point of the whole string, it has no left neighbor. But it might have a right neighbor; if that right neighbor is different color, then leftmost point has a neighbor of different color (the right neighbor), so it would be deleted! The condition: point is deleted if it has a neighbor point of a different color. So if leftmost point's only neighbor (right neighbor) is different color, then it will be deleted. So even end points can be deleted if they have neighbor of different color. Only if the neighbor is same color (i.e., the string starts with two same letters) then the leftmost point's only neighbor is same color, so not deleted. But if the first two characters are same, then leftmost point is not deleted. However, careful: if the first two characters are same, they belong to same run, so leftmost point is interior? Actually the leftmost point is the leftmost of run; it has no left neighbor, but right neighbor is same color, so it does NOT have neighbor of different color. So safe. So only points that have at least one neighbor of different color get deleted.
So in a run of length L:
- If L >= 3: interior points (positions 2..L-1) have both neighbors same color (since within run), so they are safe.
- The leftmost point of the run (position 1) has left neighbor (if any) and right neighbor (position 2). The right neighbor is same color, left neighbor is different (if exists) because run is maximal. So if left neighbor exists (i.e., run not at left end), then leftmost point has a neighbor of different color (the left neighbor) and gets deleted. If run at left end, leftmost point has no left neighbor; only neighbor is same color, so it does NOT get deleted.
- Similarly, rightmost point gets deleted if run not at right end.
Thus in the first operation, we delete:
- All characters that are at boundaries between runs (i.e., the first and last character of each run) except possibly at the ends of the whole string if the run touches the end? Actually run at left end: leftmost character not deleted; run at right end: rightmost character not deleted.
But also, if a run consists of a single character (L=1) that is not at either end? Actually if a single character surrounded by two different colors, then it has two neighbors both different color? They are different from itself, so it gets deleted. If it's at left end (first character) and second character is different, then it has one neighbor of different color, so deleted. So indeed, any character that has at least one neighbor of different color is deleted.
Thus first operation deletes all characters except those that are in "interior" of runs (length >=3) and also characters at ends if the runs at ends have length >=2? Actually for an end run, the end character is safe if the adjacent neighbor (if exists) is same color, i.e., if the run length at end is at least 2, then the end character has same-color neighbor, and no other neighbor, so safe. But if the run at end has length 1, then end character has neighbor of different color (since adjacent is different or maybe no neighbor? If string length=1, then no neighbor, safe). For length 1 at end: if n>1, the end character has neighbor (second character) which is different (since run length 1, neighbor different), so it gets deleted. So ends are safe only if the end block has length >=2, because then the end character has same-color neighbor and no other neighbor. So summary after first operation: we keep interior points of runs (positions that have both left and right neighbors within same run) and end points that are part of runs of length >=2 (i.e., end characters that have neighbor same color and no other neighbor). But also, what about a run of length 2 in middle? Both characters are edges: leftmost has left neighbor diff, rightmost has right neighbor diff; so both get deleted. So run length 2 entirely deleted. Run length 1 anywhere (unless isolated string) gets deleted. So after first operation, remaining characters are those that belong to runs of length at least 3, but only the interior positions? Actually interior positions are positions 2..L-1 (if indexing within run). But also, if a run touches an end, the end character is safe if L>=2? Check: For left end run length L: leftmost character (position 1) has no left neighbor, right neighbor is same color if L>=2. So it has no neighbor of different color -> safe. But rightmost character of that run (if L>=2) might be safe if the run ends at the right end? Actually if the run is at left end but not at right end, then the run's rightmost character is at boundary with a different run. That rightmost character has a right neighbor of different color, so it would be deleted. So only the endmost character that is at the absolute left end and part of a run of length >=2 is safe. Similarly right end safe. So after first operation, we keep:
- For each maximal run of same color:
- If run length L >= 3: keep positions 2..L-1 (i.e., interior) safe. Also if run touches left end, also keep position 1? Let's test: left end run, L>=2: position 1 is leftmost, no left neighbor, right neighbor same color, so safe. But does position 1 get deleted? According to rule, it is not deleted because no neighbor of different color. So it remains. However, consider left end run length L=2: positions 1 and 2. Position 1: left neighbor none, right neighbor same color -> safe? Wait right neighbor same color, so no different neighbor, so it should be safe. But earlier we said run length 2 entirely deleted. Let's examine carefully: For left end run length 2, string: e.g., "aab...". So first char 'a', second char 'a'. The leftmost char 'a' has neighbor right 'a' same color, no left neighbor. So does it have a neighbor of different color? No. So it should NOT be deleted. But our earlier intuition said runs of length 2 are entirely deleted. That's wrong because the leftmost char might be safe if at end. Let's test with example: "aab". We have runs: "aa" length 2 at left, and "b" length 1. According to process: first operation delete points that have neighbor of different color.
Points:
- index1 'a': left neighbor none, right neighbor 'a' same => not deleted.
- index2 'a': left neighbor 'a' same, right neighbor 'b' different => has neighbor of different color => deleted.
- index3 'b': left neighbor 'a' different => deleted (right neighbor none). So after first operation, only index1 remains -> "a". Then second operation? "a" has no neighbors, so no deletion. So number of operations = 1. Let's test with our reasoning: left end run length 2: only the rightmost of that run (index2) deleted, leftmost survives. So indeed run length 2 at ends: only the interior? Actually both are edges: leftmost edge is at absolute end, so it's safe. Rightmost edge has neighbor different, so deleted. So for a run at left end, safe characters are: if L>=1, the leftmost character is safe (since no left neighbor, and right neighbor may be same if L>=2, else if L=1, right neighbor is different or none; but if L=1 and at left end, then only character? Actually if L=1 and at left end with n>1, then it has right neighbor different, so it has neighbor of different color -> deleted. So leftmost character of left end run is safe only if L>=2 (since then right neighbor same) OR if n=1 (no neighbor). So condition: at end, the end character is safe iff the run length at that end is at least 2, or if n=1.
Similarly for right end.
Now interior runs (not touching ends): both ends are boundaries, so both end characters get deleted. So only interior positions (if L>=3) survive. So after first operation, the string is transformed into a new string consisting of:
- Possibly a leftmost character if the leftmost run length >=2 (and that character is kept)
- Possibly a rightmost character if the rightmost run length >=2
- For each run (including possibly the end runs), the interior characters (positions that are not at boundaries of the run) survive, i.e., if L>=3, we keep L-2 characters from the interior. But careful: For end runs, the end character may be kept as well, which is a boundary but not considered interior. However, for end runs, the interior characters are those not at the ends of the run, but we must consider if they survive: For left end run: characters at positions 2..L (but position L is the rightmost of the run). Position L: if L>=2 and run is left end but not right end, position L is a boundary with different color, so it gets deleted. So among positions 2..L-1 (if L>=3) are interior and have both neighbors same? Actually position 2: left neighbor position1 (same), right neighbor position3 (same) if L>=3; so safe. So interior positions of left end run survive as well. But the rightmost position (L) is a boundary and gets deleted. So overall for left end run: surviving characters are: position 1 (if L>=2) and positions 2..L-1 (if L>=3). So effectively, if L>=2, keep the leftmost character; and if L>=3, keep the next L-2 characters? Actually L-2 interior positions (positions 2..L-1). So total survivors from left end run = max(0, L-1?) Let's compute: if L=1: survivors 0 (since deleted). L=2: survivors: position1 (safe) => 1. L=3: survivors: position1 and position2? Wait position2 is interior? For L=3, positions: 1(leftmost),2(middle),3(rightmost). Position1 safe (end), position2 safe (both neighbors same), position3: boundary with different? Since run is left end but not right end unless it's also right end. If left end only, position3 gets deleted. So survivors: positions1 and2 => 2 survivors. So survivors count = L-1? L=2 gives1, L=3 gives2. But L=4: positions1 safe, positions2,3 interior safe? Actually positions2 and3 are interior (both have same neighbors), position4 boundary deleted. So survivors:1,2,3 =>3 = L-1. So for left end run that is not also right end, survivors = L-1 (all but the rightmost character). Similarly, for right end run (not left), survivors = L-1 (all but the leftmost character). For a run that is both ends (i.e., whole string single run), then both ends have no neighbor of different color, so both ends are safe. In that case, all characters survive? Let's check: whole string same color, e.g., "aaa". According to condition, any point with neighbor of different color? None, so no deletions at all. So process stops at 0 operations. So we never delete any if string already uniform. So in that case, survivors = L (all). But in our transformation for first operation, if it's uniform, no deletions happen, so we don't apply operation. So we only consider non-uniform strings. So for a non-uniform string, each end run touches one end but not the other, so survivors = L-1 as above. For interior runs, survivors = max(0, L-2) because both ends are deleted. So interior run survivors = L-2 if L>=2? Actually if L=2, survivors 0; L=1, 0; L>=3, L-2.
Thus after first operation, the surviving characters are as described, and they maintain their relative order. They form a new string, which likely consists of blocks (maybe merging of survivors from adjacent runs? Since we delete boundary characters, survivors from adjacent runs might become adjacent if the boundary characters were deleted. For example, original: run A of length L_A, then run B of length L_B. After deletion, what remains from run A? Possibly some suffix (if A is left of B). Run A (on left) not end: if A is interior or left end? Actually consider two runs A and B, with A on left, B on right. After deletion, from A we keep: if A is leftmost run? Could be leftmost or interior. Let's analyze generic.
Let original string be partitioned into maximal runs: R1, R2, ..., Rk, each with color c_i and length L_i. k >= 1. If k=1, uniform, answer=0.
For k >=2, we perform operations.
Goal: compute number of operations until no more deletions.
Observation: The process resembles repeatedly "peeling off" boundary layers of runs. Each operation removes all characters that are adjacent to a different color. That is like removing all characters that are at the interface between different colors, and also characters at ends if they have a different neighbor.
This is similar to computing the number of steps needed to reduce the string to a "core" that is all same color, where each step reduces lengths of runs.
We can model each run's length over time. However, due to simultaneous deletions, survivors from adjacent runs may become adjacent if the boundary characters were removed. For example, original: "aaabbb". Runs: A len 3, B len 3. First operation: For A (left end? Actually leftmost run A touches left end, so survivors: leftmost char and interior? A len 3, left end, survivors: positions1 and2 (since position3 is boundary with B and gets deleted). So survivors from A: two a's: 'a','a'. From B (right end): survivors: positions5? Let's index: string: 1:a,2:a,3:a,4:b,5:b,6:b. For B (right end), survivors: rightmost char (6) and interior? B len3, right end, survivors: positions5 and6? Actually for right end run, survivors = L-1 =2, which are the rightmost two characters: positions5 and6? But careful: boundary is leftmost of B (position4) which gets deleted. So survivors from B: positions5 and6 (both b). So after deletion, we have survivors: indices 1,2 from A and 5,6 from B. But also, what about position? They are not contiguous: after deleting positions3 and4, the survivors become: 1,2,5,6 with a gap. The new string order: 1,2 then after gap we have 5,6 -> they become adjacent? Actually after removal of positions 3 and4, the survivors become contiguous: indices1,2 become positions 1 and2 in new string; indices5,6 become positions3 and4? Because after deletion, the remaining points shift to fill the line? The problem states: "delete all points ... After that you can perform the next operation etc." When points are deleted, the remaining points remain on the line, preserving order, and become neighbors accordingly. So indeed after deletion, the gaps close up. So the new string would be: survivors from A (two a's) followed by survivors from B (two b's) -> "aabb". So now we have runs: "aa" and "bb". So first operation turned "aaabbb" into "aabb". That's interesting: effectively, the runs shrink by 1 on each side? But careful: For A (left end), it lost its rightmost character; for B (right end), it lost its leftmost character; the middle boundary characters (both at interface) got deleted (A's last and B's first). So the lengths become: L_A' = L_A - 1 (since left end run loses 1 at right boundary, but keeps leftmost and interior; L_A' = L_A - (deleted at right boundary) = L_A - 1? Actually from A, survivors = L_A - 1 if left end and not right end? For L_A=3, survivors 2 = L_A-1. So yes, for a left end run, new length = L_A - 1 (provided L_A >=1; if L_A=1, survivors 0). For a right end run, new length = L_B - 1. For interior runs (not touching ends), they lose both ends, so new length = max(0, L_i - 2). In our example, both were end runs (since k=2, both are ends). So after first op, we got "aabb": runs: 'a' len2 (left end) and 'b' len2 (right end). Now second operation: For left end run len2: leftmost char safe (since left end and right neighbor same) -> survivors: leftmost char only. Right end run len2: rightmost char safe -> survivors: rightmost char only. So after second op, we get "ab"? Actually "aabb": positions: a,a,b,b. Delete points with neighbor of different color: First char 'a': left neighbor none, right neighbor 'a' same => not deleted. Second char 'a': left neighbor 'a' same, right neighbor 'b' diff => deleted. Third char 'b': left neighbor 'a' diff, right neighbor 'b' same => deleted. Fourth char 'b': left neighbor 'b'? after deletion? Actually we need to base on original configuration before deletion. For third char 'b', left neighbor is second 'a' (diff) so deleted. Fourth char 'b', right neighbor none, left neighbor 'b'? Wait left neighbor is third 'b', but third 'b' is same? Actually third char is 'b', so left neighbor of fourth is third 'b' (same), so fourth char has left neighbor same and no right neighbor => not deleted. So survivors: first and fourth => "ab". Then third operation: "ab": both have neighbor of different color (a's only neighbor b diff, b's only neighbor a diff) => both deleted, leaving empty. So total operations = 3? But example "aaabbb" not given; let's compute expected? Process: Starting "aaabbb". Operation1: deletes positions3 and4? Actually we need to recalc: "aaabbb": Let's compute deletions systematically. Points:
1: left none, right 2 (a) same -> no deletion (no neighbor diff).
2: left 1 (a) same, right 3 (a) same -> no.
3: left 2 (a) same, right 4 (b) diff -> deleted.
4: left 3 (a) diff, right 5 (b) same -> deleted.
5: left 4 (b) diff, right 6 (b) same -> deleted? Wait 5: left neighbor 4 (b?) Actually 4 is b? No, original string: indices:1:a,2:a,3:a,4:b,5:b,6:b. So 5's left neighbor is 4 (b) same, right neighbor 6 (b) same. So 5 does NOT have neighbor of different color. So it should NOT be deleted. 6: left neighbor 5 (b) same, right none -> not deleted. So earlier I incorrectly said 5 gets deleted. Let's correct: For point5, left neighbor 4 is b (same), right neighbor 6 is b (same), so it's safe. Only points with neighbor of different color: point3 (a with neighbor b), point4 (b with neighbor a). That's it. So survivors: points1,2,5,6 -> "aabb". So indeed after first op we get "aabb". Good.
Now second op on "aabb": we computed survivors: points1 and4? Actually "aabb": indices:1:a,2:a,3:b,4:b. Determine deletions:
1: left none, right 2 (a) same -> safe.
2: left 1 (a) same, right 3 (b) diff -> delete.
3: left 2 (a) diff, right 4 (b) same -> delete.
4: left 3 (b) diff? But we base on original config before deletion; left neighbor is 3 (b) same? Wait original before deletion: 3 is b, so left neighbor of 4 is 3 (b) same, right none -> safe.
So survivors:1 and4 -> "ab". So second op yields "ab".
Third op on "ab": both have neighbor diff -> both deleted, survivors: empty. So total operations = 3. So for "aaabbb", answer would be 3. Let's test manually: seems plausible.
Now this process is like repeatedly "eroding" runs from boundaries and also merging adjacent runs? After each operation, runs may merge if adjacent survivors from different runs become adjacent. For example, after first op on "aaabbb", we had "aabb" which is two runs of a and b. After second op, we got "ab" which is two runs of length1 each? Actually "ab" is two runs: a and b of length1 each. Then third op deletes both. So the number of operations seems related to the maximum "distance" from each character to a boundary where color changes.
Alternative perspective: This process can be seen as each point having a "time" at which it gets deleted. At each operation, any point that has at least one neighbor of a different color gets deleted. This is similar to a cellular automaton where a point survives if both its neighbors (if exist) are same color as itself. So we want the number of rounds until no more deletions, i.e., the maximum deletion time among points (since process stops after all deletable points are gone). Actually if points get deleted over rounds, the process stops when all remaining points have same color. The number of operations is the maximum number of rounds needed for any point to be deleted (or the number of rounds until stable). Actually if some points survive forever (final set), they never get deleted; they are not counted in rounds. So the number of operations is the number of rounds until no further deletions; that is the maximum "time" at which any deletion occurs (since after that round, no deletions happen). But note that deletions occur in rounds; if the maximum deletion round is T, then we performed T operations because after T-th operation, maybe some points survive, but the (T+1)-th operation would have no deletions. So answer = max deletion time among points that are eventually deleted? But also points that never get deleted have infinite time, but they cause process to stop early? Actually if some points survive forever, then after the round where last deletion occurs, the process stops; so the number of operations is the maximum deletion time among points that get deleted, provided there is at least one deletion. If no deletions ever (uniform string), answer=0.
So we can assign to each character an integer t (starting from 1) indicating the operation in which it gets deleted, or infinity if never deleted. Deletion rule: At round r, a character is deleted if at the beginning of that round, it has at least one neighbor of a different color. Equivalently, a character survives to next round if both its neighbors (if any) are the same color as itself at the current round.
This is reminiscent of the concept of "distance to the closest character of different color" in the initial string, but due to dynamic changes as neighbors get deleted, it's more complex. However, there is known result for this type of process: it's equivalent to each character's "time to death" being the minimum number of steps for a different color to reach it from either side, akin to propagation of "infection" from boundaries between colors. Actually similar to problem "String Game" or "Removing from ends" like "We repeatedly remove characters that have a neighboring character with a different color". This is exactly the process of "predator" where each character's deletion time equals the distance to the nearest position where the color changes, but with some nuances because deletions happen simultaneously and can cause merging of colors.
I recall a Codeforces problem: "C. Binary String Removing" maybe? Actually there's a problem "C. Letters Removal" no. Another known problem: "Elimination" or "String Elimination" maybe similar. Actually I've seen a problem: "D. Delete a Substring"? Not that.
Consider modeling each character as having a "time" until it becomes "stable" (i.e., both neighbors same color). The process is like: initially, characters at boundaries of color blocks (i.e., where adjacent characters differ) are deleted in first round. After they are gone, new boundaries appear between runs that were previously separated by a buffer of deleted characters. It's akin to a multi-phase erosion.
This is similar to the concept of "run-length encoding" and "distance to nearest different color" on a line, but as deletions happen, the distances shrink. Actually there is known simplification: The deletion time for a character equals the minimum distance to a character of a different color in the initial string, but not exactly. Let's test on simple strings.
Example 1: "aabb" -> answer 2. Let's compute distances to nearest different color:
Positions: 1:a: nearest different at pos2? Actually pos2 is a, same; next pos3 is b (diff) distance 2. So min distance = 2.
2:a: nearest diff: pos3 (b) distance 1.
3:b: nearest diff: pos2 (a) distance 1.
4:b: nearest diff: pos3 (a?) Actually pos3 is b, same; pos2 is a distance 2.
If we claim time = distance? Then max distance = 2, answer would be 2, matches. Example 2: "aabcaa" -> answer 1. Distances: Let's compute: string: a a b c a a.
Pos1 a: nearest diff? pos3 b distance 2, pos? pos2 same, so min distance 2.
Pos2 a: nearest diff pos3 b distance 1.
Pos3 b: nearest diff pos2 a distance 1 or pos4 c distance 1 -> 1.
Pos4 c: nearest diff pos3 b distance 1 or pos5 a distance 1 -> 1.
Pos5 a: nearest diff pos4 c distance 1.
Pos6 a: nearest diff pos5? same, pos4 c distance 2 -> 2.
Max distance = 2, but answer is 1. So distance alone not correct.
Maybe time equals distance to the nearest "boundary" where color changes, but with a twist: when multiple deletions happen, some interior points may become boundary earlier. In "aabcaa", the string: runs: "aa", "b", "c", "aa". So runs lengths: 2,1,1,2. First operation: what gets deleted? According to earlier rules:
- Left end run "aa" (length2): leftmost safe, rightmost deleted? Since rightmost has neighbor b diff, so deleted. So from "aa", survivors: leftmost 'a'.
- Run "b" length1: interior? It's interior (both sides different), so deleted entirely.
- Run "c" length1: deleted entirely.
- Right end run "aa" length2: leftmost deleted (since neighbor c diff), rightmost safe. So survivors: rightmost 'a'.
So after first op, survivors: leftmost a and rightmost a -> "aa". Now they are same color, so no further deletions. So answer 1. So max distance to different color was 2 but only 1 operation needed. So time is not simply distance.
Consider another: "ab" -> distances: a:1, b:1 -> max 1 -> answer? Process: first op both deleted -> answer 1. So matches.
"aba": distances: a at pos1: nearest diff pos2 b distance1; pos2 b: nearest diff pos1 or 3 distance1; pos3 a: nearest diff pos2 distance1 -> max1 -> answer? Process: "aba". Determine first op: positions: 1 a: neighbor 2 b diff -> delete; 2 b: neighbors a and a both diff? Actually neighbors are a and a: both different? b vs a are diff, so delete; 3 a: neighbor 2 b diff -> delete. So all deleted in first op, answer 1. So matches.
"abba": distances: a1: diff at pos2 distance1; b2: diff at pos1 or pos3? pos3 b same, pos1 a diff distance1; b3: diff pos4 a distance1; a4: diff pos3 distance1. max1 -> answer? Process: "abba". first op: Check each: 1 a: neighbor 2 b diff -> delete; 2 b: left 1 a diff, right 3 b same -> delete; 3 b: left 2 b same? Actually left 2 b same, right 4 a diff -> delete; 4 a: left 3 b diff -> delete. So all deleted in first op, answer1. matches.
"aab": distances: a1: nearest diff? pos3 b distance2; a2: nearest diff pos3 b distance1; b3: nearest diff pos2 a distance1 -> max2 -> answer? Process: earlier we computed "aab" -> after first op, only a1 remains? Let's recalc: "aab": positions 1:a,2:a,3:b. First op: point1 a: left none, right a same -> safe. point2 a: left a same, right b diff -> delete. point3 b: left a diff -> delete. So survivors: point1 a. Then second op: only a, no neighbor -> no deletion. So only 1 operation. So answer1, not 2. So max distance gave 2 but answer1. So time not distance.
Thus we need a more accurate model.
Let's think in terms of runs and their lengths. The process seems to shrink runs from the ends and from the boundaries between runs. After each operation, runs may merge if adjacent runs become the same color after deletions? Actually after deletions, survivors from adjacent runs may be of different colors, so they remain separate runs. In "aab", after first op we had "a", which is one run. So originally runs: "aa", "b". After op, we got "a". So runs merged? Actually original left run "aa" contributed 'a' (leftmost), and right run "b" contributed nothing. So resulting string is just 'a' from left run. So it's not merging of runs of same color; it's just that one run survived partially.
In general, after an operation, the surviving characters from two adjacent runs (original) may become adjacent if the boundary characters (which were of two different colors) were deleted. They will be of different colors (since each run's survivors keep the same color). So they form a new boundary between runs. For example, "aaabbb" gave "aabb" -> two runs of a and b. So runs remain distinct.
Thus the process can be modeled on the initial run lengths. Each operation reduces the lengths of runs, and may eliminate some runs entirely. Eventually, we may be left with one or more runs of same color? Actually final state all same color, so only one run remains. So the process continues until either all runs disappear or one run remains (and its color uniform). The number of operations is the number of rounds needed to reduce the number of runs to 1 (or 0) by shrinking runs from ends and from interior boundaries.
Let's formalize. Represent the string as sequence of runs with colors and lengths: (c1, L1), (c2, L2), ..., (ck, Lk). k>=1.
Define operation: For each run i:
- If i = 1 (leftmost), then the run loses its rightmost character (if Li >= 1) because that character has a neighbor of different color on the right (unless k=1, but if k>1, there is a right neighbor run). But also, the leftmost character of the run might be deleted if it has a left neighbor of different color, but since it's leftmost, it has none. However, if Li >= 2, the leftmost character is safe; if Li = 1, then the single character is at leftmost but also right neighbor different? Actually if Li=1 and k>1, then that single character has right neighbor different, so it will be deleted. So the run loses its leftmost character? Wait, leftmost run: For each character in the run, deletion condition based on neighbors before deletion. The leftmost character (position 1 of run) has no left neighbor, and its right neighbor is within run if Li>=2, else if Li=1, then right neighbor is from next run (different). So:
- If Li >= 2: leftmost character is safe (no left neighbor, right neighbor same). The second character? It has left neighbor same, right neighbor same if Li>2, else if Li=2, second character's right neighbor is from next run (different), so it gets deleted. So for leftmost run, all characters except possibly the leftmost survive? Let's enumerate: For leftmost run (i=1) with length L:
- Character at index 1: safe if L>=2? Actually safe if it has no neighbor of different color. Its only neighbor is character2 if L>=2 (same) so safe; if L=1, neighbor is character from run2 (different) so not safe. So safe condition: L >= 2.
- Character at index j (2 <= j <= L-1): these have both neighbors within run (same), so safe regardless (provided L>=3 for interior existence). Actually if L>=3, j=2 has left neighbor1 (same) and right neighbor3 (same) -> safe. So all interior characters are safe.
- Character at index L (rightmost): left neighbor is character L-1 (same if L>=2), right neighbor is from next run (different if i<k). So has neighbor of different color -> deleted. So rightmost is always deleted if there is a next run (i.e., i<k). So for leftmost run, survivors are: character1 if L>=2, and characters 2..L-1 (i.e., interior) if L>=3. So survivors count = max(0, L-1) when k>1? Because if k>1, we lose the rightmost always. But if L=1 and k>1, then survivors=0. If L>=2, survivors = L-1. That matches earlier: leftmost run loses exactly 1 from the right end, but retains leftmost and interior.
Similarly, for rightmost run (i=k), survivors = L-1 (lose leftmost character) if k>1? Actually symmetric: leftmost character deleted (since left neighbor different), rightmost safe if L>=2. So survivors = L-1 when L>=2, else 0.
For interior runs (1 < i < k), both ends are adjacent to different colors, so both leftmost and rightmost characters are deleted. The interior characters (positions 2..L-1) are safe if L>=3; if L=2, both characters are ends and get deleted -> survivors 0; if L=1, deleted ->0. So survivors = max(0, L-2).
Thus after one operation, the new string is formed by concatenating survivors from each run in order. Notice that survivors from adjacent runs may now be directly adjacent, with colors c_i and c_{i+1} (since the boundary characters that were of different colors are gone). So the new string will have runs corresponding to the survivors from each original run, but note that if a run's survivors are empty, that run disappears. However, if two consecutive runs both have survivors, they will be adjacent and of different colors, so they remain separate runs. So the new runs are just the survivors of each original run, possibly with some runs removed.
Thus the transformation on run lengths is: For each i:
- If i == 1 (leftmost): new length L_i' = max(0, L_i - 1)
- If i == k (rightmost): new length L_i' = max(0, L_i - 1)
- Else: new length L_i' = max(0, L_i - 2)
provided that k>1. If k=1 (uniform), no operation occurs (we stop), but if we apply formula, L_i' = L_i (since no ends are adjacent to different colors) but we wouldn't apply operation. So we only apply when there is at least two runs.
After this transformation, we may have new runs where some L_i' = 0, which means that run disappears. Then we recompute runs (colors unchanged) and k' = number of runs with positive length. The process continues.
But careful: This transformation is valid only if we assume that after each operation, the survivors from each run remain contiguous and their order preserved, and no merging of same-color runs occurs because survivors from adjacent runs are of different colors. However, it's possible that after reduction, a run's survivors become zero, and the runs on either side (if they exist) are of different colors, so they become adjacent but of different colors, so they remain separate. No merging of same color because adjacent runs originally have different colors, and survivors keep the same colors. So the runs remain distinct colors. Thus the process can be modeled purely on the sequence of run lengths (with colors irrelevant except to differentiate runs). Since colors don't matter beyond distinguishing runs, we can treat runs as abstract items with lengths, and each operation reduces each length by a certain amount: 1 for end runs, 2 for interior runs. This continues until either only one run remains (with any length) or all runs vanish (i.e., empty string). However, careful: When only one run remains (k=1), the process stops (no further deletions). Also if all runs vanish (k=0, i.e., empty string), then after that operation there are no points, so next operation cannot be performed; so that is also termination. But note: if we get to a state with one run of length L, that run is uniform and no further deletions occur. So number of operations performed equals number of times we applied the reduction until k <= 1.
But is this transformation accurate for all cases? Let's test on examples.
Example 1: "aabb" -> runs: ('a',2), ('b',2). k=2.
Apply reduction: leftmost run (a) L1' = max(0,2-1)=1. rightmost run (b) L2' = max(0,2-1)=1. New lengths: [1,1] with colors a,b. So new string "ab". That's after first operation. Now we have runs: ('a',1),('b',1). k=2 again. Apply again: leftmost L1'=max(0,1-1)=0, rightmost L2'=max(0,1-1)=0. So after second operation, both lengths become 0, i.e., empty string. So operations =2. matches example 1.
Example 2: "aabcaa" -> runs: a:2, b:1, c:1, a:2. k=4.
Apply reduction:
i=1 (leftmost) L1' = max(0,2-1)=1
i=2 (interior) L2' = max(0,1-2) = max(0,-1)=0
i=3 (interior) L3' = max(0,1-2)=0
i=4 (rightmost) L4' = max(0,2-1)=1
New runs: [1 (a), 1 (a)]? Wait colors: after reduction, runs that survive: run1 (color a) length1; run4 (color a) length1. Since run2 and run3 disappeared, we now have two runs both color a? But careful: run1 is color 'a', run4 is color 'a'. After run2 and run3 disappear, run1 and run4 become adjacent. Since they are same color, they should merge into a single run of length 2. But our earlier assumption said no merging of same color because adjacent runs originally have different colors. However, after interior runs disappear, it's possible that two runs of same color become adjacent, merging into one run. In this example, run1 color 'a' and run4 color 'a' are separated by runs of different colors (b and c). When those runs are completely removed, the two 'a' runs become adjacent, and since they are same color, they merge into a single run. That's important! Our simplification of just reducing lengths independently and keeping runs separate fails when two runs of the same color become adjacent after intermediate runs vanish. So we need to account for merging of same-color runs.
Thus the process is more complex: after each operation, we need to re-group contiguous survivors of the same color into runs. So the transformation on run lengths must include merging of adjacent runs of same color when the runs between them are eliminated.
Thus we need a simulation that accounts for runs and their lengths, and at each step we reduce lengths as described, then merge adjacent runs of same color. However, we cannot simulate step by step up to maybe O(n) operations? The number of operations could be up to O(n) (like "ababa..." pattern may cause many operations). But n up to 1e6, we need O(n) or O(n log n) total.
We need to compute the number of operations without explicit simulation of each operation (which could be up to n). But maybe the number of operations is bounded by something like O(log n) or O(sqrt(n))? Not necessarily; consider a string like "ab" repeated many times: "ababab...". For n=2k, runs: each single character run alternating a and b, so k runs each length 1. In each operation, interior runs (those not at ends) have length 1, so they become max(0,1-2)=0, i.e., deleted. End runs (leftmost and rightmost) become max(0,1-1)=0? Actually end runs length 1: leftmost length1, reduction gives 0 because L-1=0. So all runs become 0 after first operation? Let's test "ab": first operation deletes both -> 0. So for "abab" (a,b,a,b): runs: a1,b1,a1,b1. Apply reduction: leftmost a: 1-1=0; interior runs: b: 1-2=-1->0; a: 1-2=-1->0; rightmost b: 1-1=0. So all zero, string empty after 1 operation. So many runs can disappear quickly. But consider "aabb" gave 2 operations. Consider "aaabbb" gave 3 operations. Consider "aaa...abbbb..." with large runs. Let's test "aaaabbbb": a:4, b:4. After op1: a' = 4-1=3, b' = 4-1=3 -> "aaabbb"? Actually survivors: a: leftmost and interior: positions1,2,3? leftmost run length4: survivors=3 (since lose rightmost). b length4: survivors=3 (lose leftmost). So new string: "aaabbb". That's runs: a:3, b:3. Op2: a' =3-1=2, b'=3-1=2 -> "aabb". Op3: a'=2-1=1, b'=2-1=1 -> "ab". Op4: both become 0 -> empty. So total operations =4. So with runs of length L, operations roughly L? Actually L=4 gave 4 operations? Let's compute: L=4 gave 4 operations. "aaabbb" (L=3) gave 3 operations. "aabb" (L=2) gave 2 operations. "ab" (L=1) gave 1 operation. So pattern: for two runs of equal length L, answer = L? Let's test L=5: "aaaaabbbbb". Op1: a:4, b:4 -> "aaaabbbb"? Actually after op1: a:5-1=4, b:5-1=4 => "aaaabbbb". That's two runs lengths 4,4. Then we continue as before, so total operations =5? Because we need to reduce from 5 to 0, each operation reduces each run by 1? But after first op, both become 4; then 3,2,1,0? That's 5 operations? Let's simulate quickly: Start (5,5). Op1->(4,4). Op2->(3,3). Op3->(2,2). Op4->(1,1). Op5->(0,0). So 5 operations. So yes, answer = L for two runs of equal length L. So operations can be up to n/2? Actually max length of run could be up to n, but if there are many runs, the operations might be limited by the longest run? Possibly the number of operations is determined by the maximum "radius" of something.
But consider more complex patterns with multiple runs. Example "aabcaa" had runs lengths 2,1,1,2 and answer 1. So merging occurred, reducing number of operations.
Thus we need a method to compute number of operations efficiently.
We can view each character as having a "deletion time" which is the number of operations after which it is removed (or infinity if it survives forever). The process can be seen as: at each step, characters that are adjacent to a different color get deleted. This is akin to a "propagation" of deletion from color boundaries inward. Think of each maximal monochromatic segment. The boundaries between segments are initially active: the characters at the boundaries (i.e., the first and last character of each segment) will be deleted at step 1, except possibly those at the ends of the whole string if the segment is long enough? Actually as we determined, end characters are deleted only if they have a neighbor of different color, i.e., if the segment is length 1 at an end, or if the segment is longer but the end character has neighbor of same color? Wait, at ends, the end character gets deleted if it has a neighbor of different color. For a left end segment of length L, the leftmost character has no left neighbor; its right neighbor is same if L>=2, so safe. So only if L=1 and there is another segment to the right, then it gets deleted. So ends are "protected" if the end segment length >=2. So ends can survive first step. Similarly interior segments have both ends vulnerable.
So deletion propagates from the boundaries between segments inward, and from ends if they are not protected.
This is reminiscent of the problem where we simulate erosion of segments, and the deletion time for a character equals the minimum distance to a "boundary" where a different color exists, but with the twist that when segments merge, boundaries disappear.
Let's formalize: Let the initial string be represented as runs: colors c[1..k] and lengths L[1..k]. We'll treat each run as an interval of positions. Over time, each run shrinks from both ends (if interior) or from one end (if at boundary), unless it merges with a neighboring run of the same color when the separating run(s) disappear.
The merging event can happen when an interior run's length reduces to zero, making adjacent runs of possibly same color touch. If they are same color, they merge, forming a larger run. This larger run may then be longer, affecting subsequent shrinkage rates (since after merging, the new run might be interior or at ends). For example, "aabcaa": runs: a2, b1, c1, a2. After first operation, b and c disappear (lengths become 0), and the two a runs become adjacent and merge into a single run of length 2 (since both a). Then we have a single run (k=1), so process stops. So answer = 1.
Thus we need to simulate the shrinkage and merging process efficiently. Since n up to 1e6, we can perhaps simulate using a queue or list of runs, and at each operation, we subtract 1 from each end run and 2 from interior runs, then remove zero-length runs, and merge adjacent runs of same color. This could be done iteratively, but the number of operations could be up to O(n) (e.g., two runs of length n/2 yields n/2 operations). That would be O(n^2) if we simulate each step individually. But maybe we can compress time: we can compute for each run how many operations it will survive before being eliminated or merging, and then compute the overall number of operations as something like the maximum over some "effective distance".
Think of each run as having a "lifetime" under the shrinkage rates, but when runs merge, the combined run may have a longer lifetime because it becomes an end run after merging, which shrinks slower (only 1 per step) than interior runs (2 per step). So merging can prolong the process.
For example, in "aabcaa", the two a runs each shrink at rate 1 (since they are end runs) and they are of length 2 each. After 1 step, each loses 1, becoming length 1 and 1, and the interior runs (b,c) disappear (they shrink at rate 2, but length 1 so they vanish). Then the a runs merge to form a run of length 2 at the ends? Actually after step1, we have a (length1) from left, and a (length1) from right, they merge to form a single run of length2, which is now the whole string. Since it's uniform, process stops. So only 1 step.
If we had longer runs on ends and small interior runs, they may merge early, reducing steps.
So to compute the total number of operations, we need to simulate the process as an event-driven system where runs shrink and merge.
We can think of each run as having two "sides" that are exposed to different colors. Initially, for run i:
- Left side: if i>1, then left neighbor is run i-1 of different color. So the left boundary of run i is "active" meaning that the leftmost character of run i will be deleted at each step? Actually in each operation, the leftmost character of an interior run is deleted (since it has left neighbor different). Similarly, rightmost character is deleted. So interior runs shrink by 2 per step (one from each end). For end runs, only the interior side is active (the side facing the other run), so they shrink by 1 per step (lose the character at the boundary). The outer side (the end of string) is inactive, so no deletion from that side.
When a run disappears (length becomes 0), its neighbors (if any) become adjacent. If those neighbors have same color, they merge, forming a new run whose length is the sum of the two runs' lengths at that moment. This new run may be interior or end depending on position.
Thus we have a system of runs that shrink over time. Each run has a current length and a shrinkage