We need to process each test case, given a string s of D and K of length n. For each prefix i (1-indexed), we need to compute the maximum number of pieces we can split that prefix into (i.e., partition the prefix into contiguous segments each of length >=1) such that every segment has the same ratio (D count : K count). Equivalent to: Given a sequence, we want to partition it into as many segments as possible where each segment has the same ratio (in lowest terms). This is reminiscent of problem "CF 1490E? Actually it's Codeforces Round # 1530? I recall problem "Accidental Victory"? No, it's similar to CF problem "Diluc and Kaeya" or something? Actually title "Diluc and Kaeya" problem? Wait, the description mentions Diluc and Kaeya. Indeed the problem appears to be Codeforces Round # 1530 (Div. 1+2) problem "Diluc and Kaeya". The problem: For each prefix, find the maximum number of blocks you can split such that each block has the same ratio of D and K. The solution uses map from reduced ratio to count.
We need to compute for each prefix the answer. Approach: Iterate through the string, maintain cumulative counts: total_D and total_K so far. For each prefix, we can compute the ratio as (total_D, total_K). To partition into maximum number of blocks with equal ratio, we want to cut at positions where the cumulative ratio (D_i, K_i) seen so far is some multiple of the base ratio? Actually, consider we want to partition into k blocks, each with ratio (d,k) in lowest terms (i.e., d/k = total_D/total_K? Wait if all blocks have the same ratio, say (d:k) in reduced form, then the total ratio (total_D: total_K) must be the same as (d:k) because sum of numerators/denominators? But careful: If each block has ratio a:b (with a and b integers, possibly not reduced, but ratio equality defined as cross product equal). For two blocks with ratios a:b and c:d where a/b = c/d, then combined ratio is (a+c):(b+d). The combined ratio equals the same ratio as the individual ones if and only if a/b = c/d = (a+c)/(b+d). Actually if a/b = c/d = r, then (a+c)/(b+d) = r as well because a = r b, c = r d => a+c = r(b+d). So the combined ratio equals the same value. So the entire prefix's ratio must equal the block ratio. So the common ratio is exactly the ratio of the whole prefix. However, careful: The ratio is defined as a:b, not necessarily reduced. The condition "ratios are equal" means cross product equality. So if we have multiple segments each with the same ratio (say in reduced form p:q), then each segment's (D_i, K_i) is some multiple of (p,q). Let segment i have counts (c_i * p, c_i * q) where c_i is a positive integer. Then total counts are (p * sum c_i, q * sum c_i). So the total ratio is also p:q (in reduced form? Actually it's also a multiple, but ratio is same). So the ratio of the entire prefix is exactly the same as the ratio of each segment (when expressed in simplest terms). So the ratio of the whole prefix is the common ratio.
Now we want to partition into as many segments as possible, each with that ratio. That means we need to cut at positions where the cumulative counts so far (D,K) are in the same proportion as the total. Specifically, if total ratio is (total_D, total_K) (maybe reduced? But they might not be coprime). At a given position j (1 <= j < i), the cumulative counts (d_j, k_j) must satisfy that (d_j : k_j) is equal to (total_D : total_K). That is, cross product equality: d_j * total_K = k_j * total_D. Moreover, if we cut there, then the segment from j+1 to i would have counts (total_D - d_j, total_K - k_j) which automatically also has the same ratio if the cumulative ratio matches. So essentially we can cut at every prefix position where the cumulative ratio equals the final ratio. The maximum number of blocks equals the number of such positions (including the whole prefix? Actually if we cut at those positions, we get segments between them. If we cut at positions with cumulative ratio equal to final ratio, then each segment from previous cut to next cut will have ratio also equal? Let's verify: Suppose we have positions 0 = p0 < p1 < p2 < ... < p_m = i such that for each cut position p_j, the cumulative counts (D_{p_j}, K_{p_j}) have the same ratio as total. Then the segment from p_{j-1}+1 to p_j has counts (D_{p_j} - D_{p_{j-1}}, K_{p_j} - K_{p_{j-1}}). Since both cumulative ratios are equal to the total ratio, say (D_{p_j} = a_j * R, K_{p_j} = b_j * R with same factor? Actually if total ratio is (D_i, K_i) = (A,B) in reduced form? But not necessarily reduced; we can write ratio as (A,B) where A and B are not necessarily coprime but represent the same proportion. The condition D_{p_j} * B = K_{p_j} * A. This implies that there exists some λ_j such that D_{p_j} = λ_j * A, K_{p_j} = λ_j * B. Because if A and B are coprime? Not necessarily; we can take A and B as the total counts, but they might have a common factor. Actually the condition D_{p_j} * B = K_{p_j} * A is equivalent to (D_{p_j}, K_{p_j}) being proportional to (A,B). So there exists a scalar λ_j (rational) such that (D_{p_j}, K_{p_j}) = λ_j (A,B). Since counts are integers, λ_j = D_{p_j}/A = K_{p_j}/B must be rational. But A and B might have gcd >1. However, we can think of reduced form: let g = gcd(A,B), then reduced ratio is (a,b) = (A/g, B/g). Then the condition D_{p_j} * B = K_{p_j} * A is equivalent to D_{p_j} * b = K_{p_j} * a. So indeed D_{p_j} is a multiple of a? Not necessarily: e.g., A=2, B=4 (ratio 1:2 reduced). If D_{p_j}=1, K_{p_j}=2 => 14=4, 22=4, condition holds. Here D_{p_j}=1, which is not multiple of A=2, but is multiple of a=1. So it's multiples of reduced numerator/denominator. So we can use reduced ratio.
Thus the positions where cumulative counts are proportional to the reduced ratio are potential cut points. The maximum number of blocks we can get is the number of times we can partition into segments each having that ratio. That corresponds to: Suppose we take all positions j (including i) where the cumulative counts are multiples of the reduced ratio. Then the number of blocks is the maximum number we can get by cutting at all such positions? But careful: If we cut at all such positions, we get segments where each segment's counts also have the same ratio? Let's see: Suppose we have positions p1 < p2 < ... < pm = i where each cumulative counts are proportional. Then the segment from p_{j-1}+1 to p_j has cumulative difference: (D_{p_j} - D_{p_{j-1}}, K_{p_j} - K_{p_{j-1}}). Since both are multiples of (a,b) with coefficients λ_j and λ_{j-1}, the difference is ( (λ_j - λ_{j-1}) a, (λ_j - λ_{j-1}) b ) which is also a multiple of (a,b). And since λ_j > λ_{j-1} (since strictly increasing counts), the coefficient is positive integer. So indeed each segment also has the same ratio. Therefore, if we cut at every position where the cumulative ratio matches the total ratio, we get the finest possible partition? Could there be a partition that yields more blocks than the number of such positions? Possibly if some segments could be further split? But if we cut at every such position, we get segments that are indivisible? Actually consider that a segment itself might be further partitioned into smaller segments with the same ratio. But if we already cut at every point where the cumulative ratio matches the total ratio, then no interior point of a segment can have cumulative ratio matching the total ratio of the segment? Wait careful: The total ratio for the whole prefix is fixed. For a segment from l to r, its total ratio must equal that of the whole prefix. If we cut at all positions where the cumulative ratio equals the whole prefix's ratio, then within a segment, there is no position j (l <= j < r) such that the cumulative ratio from start of prefix to j equals the whole prefix's ratio. However, could there be a position inside the segment such that the ratio of the subsegment from l to j equals the whole prefix's ratio? That would allow further splitting, but the condition for splitting the entire prefix into blocks of equal ratio does not require that each block's boundaries correspond to whole-prefix cumulative ratio match? Let's consider: Suppose we have a partition of the prefix into blocks, each with ratio equal to the whole prefix ratio. For each block, the cumulative counts up to the end of that block is some sum of multiples. For the first block, its counts are (c1 a, c1 b). So after first block, cumulative counts are (c1 a, c1 b). That point indeed has ratio equal to the total ratio because (c1 a, c1 b) is proportional to (a,b). So the end of the first block must be a point where the cumulative ratio equals the total ratio. Similarly, after the second block, cumulative counts are ((c1+c2) a, (c1+c2) b), which also matches the total ratio. Therefore, every cut point (including the final) must be a position where the cumulative ratio equals the total ratio. Therefore, the set of cut points is a subset of the positions where cumulative ratio equals total ratio. Hence the maximum number of blocks corresponds to using all such positions (except possibly the start?). The maximum number of blocks is exactly the number of such positions (including the final prefix) because you can cut at each such position and you'll get a valid partition? But careful: The start (position 0) is not counted. So if there are m positions among indices 1..i where the cumulative ratio equals the total ratio, then we can partition into m blocks by cutting after each such position. But note that the final position i is always such a position because total ratio obviously equals itself. So the number of such positions is at least 1. Then the maximum number of blocks is exactly the number of indices j (1 <= j <= i) such that (D_j, K_j) is proportional to (D_i, K_i). But is that always possible to cut at all these positions? That would give blocks: from 1 to first such index, then from that index+1 to next such index, etc. But is it guaranteed that between these cut points, the segments also have the ratio equal to the total? As argued earlier, yes: if cumulative counts at positions p and q are both multiples of (a,b) with coefficients λ_p and λ_q, then the segment counts are ( (λ_q - λ_p) a, (λ_q - λ_p) b ), which is proportional to (a,b). So each segment's ratio is correct. However, we must ensure that λ_q - λ_p is a positive integer. Since λ values are increasing (as counts increase) and are rational? Actually λ_j = D_j / a = K_j / b? But D_j and K_j are integers, a,b are coprime. For D_j / a to equal K_j / b, we have D_j * b = K_j * a. Since a and b are coprime, this implies D_j is multiple of a and K_j is multiple of b. Indeed if a and b are coprime and D_j * b = K_j * a, then a divides D_j * b, but since gcd(a,b)=1, a divides D_j. Similarly b divides K_j. So D_j = a * λ_j, K_j = b * λ_j for some integer λ_j. So λ_j = D_j / a = K_j / b is integer. So indeed λ_j is integer. And as we progress, λ_j is non-decreasing, and strictly increasing when we see at least one character? Actually it could stay same if we get a character that doesn't change the ratio? But if λ_j stays same, that would mean D_j and K_j unchanged, which cannot happen because each step adds either D or K, so either D or K increases. For λ to stay same, we would need both D and K to increase proportionally? If we add a D, then D increases by 1, but K unchanged. Then D_j * b =? Not necessarily equal. So λ_j only increases when we reach a position where the cumulative counts become proportional again. But it's possible that there are positions where the cumulative counts are not proportional; they exist. So the set of positions with proportional counts have increasing λ_j.
Now, if we cut at all such positions, we get segments between consecutive λ values. Each segment will have Δλ = λ_{k} - λ_{k-1} >= 1. And that segment's counts are (Δλ * a, Δλ * b). So that's fine. So the maximum number of blocks we can achieve is indeed the count of such positions. Because any partition must only cut at these positions, and we can achieve the maximum by cutting at all of them. So answer for prefix i = number of indices j (1 <= j <= i) such that (D_j, K_j) is proportional to (D_i, K_i). That is, (D_j, K_j) is a multiple (by integer) of the reduced ratio of the whole prefix.
Thus we need to compute, for each i, the count of j <= i with D_j * K_i = K_j * D_i. But careful: If D_i = 0 or K_i = 0? If D_i = 0 then ratio is 0:1? Actually ratio is 0:K_i. But ratio 0:K_i means all K's. Then the condition is that for a segment to have ratio 0:K_i, it must have 0 D's. So for the whole prefix with no D's, the ratio is 0:something? But definition says ratio is a:b where a or b can be 0 but not both. So if D_i = 0, then ratio is 0:K_i (or 0:1 after reduction? Actually reduced would be 0:1 if K_i>0 because gcd(0, K_i)=K_i, so reduced ratio is 0:1). Condition for j: D_j * K_i = K_j * D_i => D_j * K_i = K_j * 0 => D_j * K_i = 0. Since K_i > 0 (if D_i=0 then there must be some K), we need D_j = 0. So condition becomes D_j = 0. Similarly if K_i=0, ratio is D_i:0, reduced 1:0, condition becomes K_j = 0. So that aligns.
Now we need to compute for each prefix efficiently. Since total n across tests <= 5e5, O(n log n) per test is fine.
We can iterate through s, maintain cumulative D and K counts. For each i, compute the reduced ratio pair (d', k') as (D_i / g, K_i / g) where g = gcd(D_i, K_i). But note D_i and K_i can be zero; gcd(0, x) = x. So g = D_i if K_i=0? Actually gcd(0,0) is undefined but that cannot happen because not both zero. So fine.
Now we need to count among previous indices j (including i) where the reduced ratio is the same as (d', k'). But careful: The condition D_j * K_i = K_j * D_i is equivalent to (D_j, K_j) having the same reduced ratio as (D_i, K_i). Because if two pairs (D_j, K_j) and (D_i, K_i) have the same reduced ratio, then cross product equality holds. Conversely if cross product holds, then they have the same reduced ratio. So indeed it's about having the same ratio in lowest terms.
Thus for each prefix i, answer = number of times the reduced ratio appears up to i. But is that exactly? Let's test with example: s = "DDK". Indices:
i=1: D => (1,0) ratio reduced (1,0). Count of (1,0) so far =1 => answer 1. matches.
i=2: DD => (2,0) ratio reduced (1,0) since gcd(2,0)=2 => (1,0). Count of (1,0) so far =2 => answer 2? But expected output for second prefix is 2? For the example first test: "DDK" => outputs "1 2 1". Indeed prefix length 2 answer is 2. So matches.
i=3: DDK => (2,1) ratio reduced? gcd(2,1)=1 => (2,1). Count of (2,1) so far? At i=3, ratios seen: i=1 (1,0), i=2 (1,0), i=3 (2,1). Count of (2,1)=1 => answer 1. matches.
Second test: "DDDDDD" all D. For each i, ratio (i,0) reduced to (1,0). So count of (1,0) after i steps is i. So answer i. matches output: 1 2 3 4 5 6.
Third test: "DKDK". Let's compute:
i=1: D => (1,0) reduced (1,0), count=1 => answer1.
i=2: DK => (1,1) reduced (1,1), count of (1,1)=1 => answer1. Output: 1 1...
i=3: DKD => (2,1) reduced (2,1) count=1 => answer1. Output: 1 1 1 ...
i=4: DKDK => (2,2) reduced (1,1) count of (1,1) now? Previously at i=2 we had (1,1). So count=2 => answer2. Output: 1 1 1 2. matches.
Fourth test: "D" => 1. Output 1. matches.
Fifth test: "DKDKDDDDK". Let's compute manually to verify: s = D K D K D D D D K.
We'll compute prefix (D,K) and reduced:
i=1: D: (1,0) -> (1,0) count1=1 => ans1.
i=2: DK: (1,1) -> (1,1) count=1 => ans1.
i=3: DKD: (2,1) -> (2,1) count=1 => ans1.
i=4: DKDK: (2,2) -> (1,1) count=2 => ans2.
i=5: DKDKD: (3,2) -> (3,2) count=1 => ans1? Wait output fifth prefix is 1? The output given: "1 1 1 2 1 2 1 1 3". So i=5 => 1, matches.
i=6: DKDKDD: (4,2) -> gcd(4,2)=2 => (2,1). Count of (2,1) so far? (2,1) appeared at i=3. So now count=2 => ans2. output: 2 at i=6, matches.
i=7: DKDKDDD: (5,2) -> (5,2) count=1 => ans1. output: 1, matches.
i=8: DKDKDDDD: (6,2) -> gcd(6,2)=2 => (3,1). Wait (6,2) reduces to (3,1). But we haven't seen (3,1) before? (3,1) is new, count=1 => ans1. output: 1 at i=8, matches.
i=9: DKDKDDDDK: (6,3) -> gcd=3 => (2,1). Count of (2,1) so far? At i=3 we had (2,1), at i=6 we had (2,1). So now total occurrences: i=3, i=6, i=9 => count=3? But output says 3 at i=9. matches.
Thus answer is simply: for each i, compute the reduced ratio (d_i, k_i) and output the current frequency of that reduced ratio (including current) up to i.
So algorithm: For each test case, read n and string s. Initialize map from pair (d,k) to count. But we need to handle pairs efficiently. Since n up to 5e5 total, we can use map with key as a pair of ints. But we could also compute a normalized representation: Since we take ratio reduced, (d,k) with gcd(d,k)=1 (or handling zeros). Actually if both non-zero, we reduce by dividing by gcd; if one is zero, the reduced form is (1,0) or (0,1). But careful: For (d,0) with d>0, gcd(d,0)=d, reduced (1,0). So all prefixes that are all D's map to (1,0). Similarly all K's map to (0,1). So we can just compute a pair of ints (x,y) where we set x = d/g, y = k/g, with g = gcd(d,k) except when both zero not possible. That yields a unique normalized ratio.
We can store counts in an unordered_map with key as a 64-bit number, e.g., combine x and y into a 64-bit integer: x * (some large) + y, or use map<pair<int,int>, int>. Since total operations across tests O(n log n) and n total 5e5, it's fine. But we need to be careful about using map with pair<int,int> might be slower but acceptable. Usually Codeforces solutions use map.
Alternatively, we can use a dictionary with key = (d/g) and (k/g) but we need to handle zeros correctly.
Implementation details:
-
Initialize map counts: maybe using unordered_map with custom hash for pair if we want O(1) average. But with n <= 5e5 and t up to 1000, map is fine; log of 5e5 is about 19, so total operations ~ 5e5 * log(5e5) ~ 5e5 * 19 = 9.5e6, fine.
-
But we need to compute gcd each step. gcd is O(log(min(d,k))). That's fine.
Edge cases: When d=0 and k>0, reduced (0,1). When d>0 and k=0, reduced (1,0). Note that gcd(0,k)=k, so d/g = 0/k = 0, k/g = k/k =1. Similarly for d>0,k=0: gcd(d,0)=d, so d/g = 1, k/g = 0.
Thus algorithm per test:
int d=0,k=0;
map<pair<int,int>, int> cnt;
vector ans(n);
for i=0 to n-1:
if s[i]=='D' then d++; else k++;
int g = gcd(d,k);
// handle zero? gcd works fine as above.
int rd = d/g;
int rk = k/g;
cnt[{rd, rk}]++;
ans[i] = cnt[{rd, rk}];
print ans.
But wait: For d=0,k=0 cannot happen. So fine.
Now we must ensure that we output spaces and newline appropriately.
Let's test on given examples. Already matches.
But is this always correct? Let's think about cases where the ratio may be same but the λ values may not be integer? Actually we argued that if (D_j, K_j) has the same reduced ratio as (D_i, K_i), then D_j / a = integer and K_j / b = integer, where (a,b) is reduced. Indeed because D_j * b = K_j * a and gcd(a,b)=1 implies a|D_j and b|K_j. So λ_j integer. So cutting at all such j yields λ increments integer. But could there be a scenario where the total ratio is (a,b) but there exists a partition into more blocks than the count of indices with that reduced ratio? Suppose some segment can be further subdivided, but not at positions that have cumulative ratio equal to total? For a segment from l to r, its internal ratio is also (a,b). Suppose we want to split it into two subsegments. For the subsegment from l to m to have ratio (a,b), we need (D_m - D_{l-1}, K_m - K_{l-1}) proportional to (a,b). But that doesn't require that the cumulative counts up to m are proportional to the total (a,b). So it's possible that there is a partition where some cut points are not at positions where the cumulative ratio matches the total ratio? But wait, consider the whole prefix partition. Suppose we have blocks: positions: 0 = p0 < p1 < p2 < ... < pk = i, each block has ratio (a,b). Then cumulative up to p1 must be (a,b)*c1, which is proportional to (a,b). So p1 is a point where cumulative ratio equals total. Similarly p2, etc. So all cut points are indeed such positions. So any partition's cut points are subset of positions with cumulative ratio proportional to total. So the maximum number of blocks is the maximum number of such positions we can include, which is all of them. However, is it always valid to include all? That would yield blocks between each consecutive such positions. But need to ensure that each such block's ratio indeed equals the total. As argued, if p and q are consecutive such positions (i.e., both have cumulative ratio proportional to total), then the difference is also proportional. So yes. So answer = number of positions j (1 <= j <= i) where cumulative ratio equals total ratio.
Thus the approach is correct.
One nuance: For positions where the reduced ratio matches, but λ may not be integer? Actually we argued that if D_j * b = K_j * a and a,b coprime, then a divides D_j and b divides K_j. Let's prove: Since a,b coprime, there exist integers x,y such that ax+by=1. But more directly: D_j * b = K_j * a => a divides D_j * b. Since gcd(a,b)=1, a divides D_j. Similarly, b divides K_j. So λ_j = D_j / a = K_j / b is integer. So indeed integer. So all good.
Now, what about case when a=0 or b=0? For (1,0) reduced for all-D. Condition for j: D_j * 0 = K_j * 1 => 0 = K_j, so K_j must be 0. So D_j can be any positive, and K_j=0. That means all-D prefixes. In reduced ratio, they are all (1,0) as we normalized. So our counting works: For any prefix that is all D, we map to (1,0). For a prefix that is all D, positions that are also all D will be counted. For a prefix with both D and K, the condition for j includes also those j that might have K_j=0? That would not satisfy cross product unless D_i=0? Actually if total has both D and K, then any j with K_j=0 would have ratio 1:0, not equal to total's ratio which has K>0. So they won't be counted. So our mapping of (1,0) for all-D is consistent.
But careful: For a prefix with D_i>0 and K_i>0, the reduced ratio (a,b) with a,b >0. For j that also have ratio (a,b) reduced, they will be counted. So map works.
Thus solution is straightforward.
Now we need to implement for multiple test cases, sum n <= 5e5. Use fast I/O.
Implementation plan:
- Read t.
- For each test:
- read n (int)
- read string s (maybe use cin, but with large input need to be careful; but 5e5 total, fine)
- Initialize d=0,k=0.
- map or unordered_map with key as pair<int,int>. Use map.
- vector ans(n).
- for i in range(n):
if s[i] == 'D': d++; else k++;
int g = gcd(d,k);
int rd = d / g;
int rk = k / g;
auto key = make_pair(rd, rk);
cnt[key]++;
ans[i] = cnt[key];
- Output ans separated by spaces and newline.
We must ensure that for d=0 and k=0, but that's not possible. For d=0,k>0: g = k, rd=0, rk=1. For d>0,k=0: g = d, rd=1, rk=0.
Double-check gcd function: C++17 has std::gcd in . Use #include . Or write own.
Complexities: O(n log n) per test due to map operations. But overall total O(N log N) where N total length. Since N=5e5, log factor maybe 19, map overhead okay. However, using unordered_map with custom hash could be faster but not needed.
But there is a nuance: The key pair is of two ints that can go up to n (5e5). So we could also compress by using int64: e.g., (int64)rd * (n+1) + rk, but careful about collisions? Since rd and rk up to n, and n+1 ~5e5, product up to 2.5e11 fits in int64. But using map with int64 is fine too. However, map is also logarithmic with int64 keys. Might be slightly faster than pair<int,int>. But not necessary.
Now we must output the answers for each test case. Use printf or cout with '\n' at end.
Now confirm with more tests:
Edge case: n=1, s='K' => d=0,k=1 => rd=0,rk=1 count =1 => ans=1. Reasonable.
Case: s="KDD". Prefixes:
i=1: K => (0,1) => (0,1) count1 ->1
i=2: KD => (1,1) => (1,1) count1 ->1
i=3: KDD => (2,1) => (2,1) count1 ->1. So output 1 1 1. Is that correct? Let's think: prefix "KD" cannot be split more than 1? Actually "KD" ratio 1:1, could split into "K" and "D"? each has ratio 0:1 and 1:0, not equal to 1:1. So only 1 block. Good. prefix "KDD": ratio 2:1. Could we split? Positions: index1: K (0,1) ratio 0:1 not 2:1; index2: KD (1,1) ratio 1:1 not 2:1; index3 is total. So only 1 block. Yes.
Case: s="DDKDDK". Let's compute:
i1:D => (1,0) -> (1,0) count1 ->1
i2:DD => (2,0) -> (1,0) count2 ->2
i3:DDK => (2,1) -> (2,1) count1 ->1
i4:DDKD => (3,1) -> (3,1) count1 ->1? Actually (3,1) reduced, count1 ->1
i5:DDKDD => (4,1) -> (4,1) count1 ->1
i6:DDKDDK => (4,2) -> (2,1) reduced? gcd=2 => (2,1) count now: previously (2,1) at i3, so count=2 => ans2.
Check if we can partition prefix6 into 2 blocks? Yes, maybe split after index3? Because after index3 we have (2,1). Then remainder from 4-6 is (2,1). So 2 blocks. Could we split into more? Could we split after index3 and also somewhere else? After index3 cumulative (2,1), after some other index? Index5 cumulative (4,1) not proportion (2,1) because (4,1) not multiple? Actually (4,1) ? cross: 41=4, 1*2=2 not equal, so not proportional. So only index3 and index6. So 2 blocks. Good.
Thus solution stands.
Now we need to output answers separated by spaces and newline after each test case. The example output shows a space after each number and also a space at the end before newline? Actually they have spaces between numbers and a trailing space. Typically it's acceptable to output with spaces and then newline, but we can avoid trailing space. We'll output without trailing space: for i from 0 to n-1: if i>0 print space; print ans[i]; then newline.
Now consider large constraints: sum n up to 5e5, t up to 1000. Use vector ans of size n per test, but we can also output directly after computing if we want to avoid storing, but need to output in order; we can compute and store ints then output. Since total stored across tests might be high but sum n is 5e5, okay.
Potential pitfalls: map of pair<int,int> for each test case; we need to clear for each test. But we can declare map inside loop; that's fine.
Now, about integer division: d/g and k/g are integers because g divides both. For d=0, g = k, d/g = 0, fine; for k=0, k/g = 0.
Now, let's double-check the condition for zero-case: For all K prefix: (0,k) reduced (0,1). The counts of (0,1) increments for each prefix that is all K (since any prefix that has only K's will have d=0 and k>0, thus reduced (0,1)). For a prefix that is all K (i.e., only Ks so far), the answer is number of prefixes that are all K so far. That is correct: For "KKK", you can split into three blocks? Actually string "KK" can be split into 'K' and 'K' each ratio 0:1, so 2 blocks; prefix "K" ->1; prefix "KK" ->2; prefix "KKK" ->3; matches our rule.
Now, confirm with mixed zeros: For prefix "DKK": (1,2) reduced (1,2). Counting previous: i=1: D (1,0) -> (1,0); i=2: DK (1,1) -> (1,1); i=3: DKK (1,2) -> (1,2) count1 ->1. So answer for prefix3 is 1. But could we split "DKK" into more than 1 block? Blocks: 'D' (1:0) and 'KK' (0:2) ratios not equal; or 'DK' (1:1) and 'K' (0:1) not equal. So only 1 block. Good.
Now, what about prefix "DDKK": (2,2) reduced (1,1). Previous occurrences: i=2 (DD->(1,0) no), i=3 (DDK->(2,1) no), i=4 (DDKK->(1,1)). Wait i=2? Actually i=1:D (1,0); i=2:DD (1,0); i=3:DDK (2,1); i=4:DDKK (2,2) reduced (1,1). Count of (1,1) =1? But we had earlier at i? Did we have (1,1) at i? In "DDKK", we have i=2? Actually string "DDKK": positions: 1:D ->(1,0); 2: D ->(2,0) ->(1,0); 3: K ->(2,1) ->(2,1); 4: K ->(2,2)->(1,1). So count of (1,1) is 1. Answer 1. But can we split "DDKK" into 2 blocks? Let's see: split after 2: first "DD" (2:0) ratio 1:0, second "KK" (0:2) ratio 0:1, not equal. Split after 1: "D" and "DKK": (1:0) vs (1:2) not equal. Split after 3: "DDK" (2:1) vs "K" (0:1) not equal. So only 1 block. Good.
Now consider prefix "DKDKDK"? Actually that might have more occurrences.
Thus solution is fine.
Now about memory: map size grows up to number of distinct reduced ratios encountered, which is at most n, but could be O(n). But total across test cases sum n <= 5e5, so fine.
Edge: t up to 1000 but each test small, map overhead per test might be high if many tests; but total n is limited, so total number of map operations is sum n. So overall complexity is okay.
Now implement.
But I'm thinking: Actually the answer is simply the frequency of the reduced fraction (d/g, k/g) among prefixes up to i. So we can just compute the fraction and update a map.
Now I'll produce final answer.
But we should also consider that the problem might require using 64-bit ints for intermediate multiplication? In condition D_j * K_i = K_j * D_i, we could avoid overflow because D_j and K_i up to n <= 5e5, product up to 2.5e11 fits in 64-bit. But we don't need that condition; we use reduced ratio.
Now about implementation of gcd: use __gcd or std::gcd.
Now test with sample:
Input:
5
3
DDK
6
DDDDDD
4
DKDK
1
D
9
DKDKDDDDK
Our code should produce:
1 2 1
1 2 3 4 5 6
1 1 1 2
1
1 1 1 2 1 2 1 1 3
Make sure formatting: each line separate, with spaces between numbers, no trailing spaces needed. The sample output has spaces after each number and a space before newline; but we can output without trailing space.
Now confirm the fifth test: Our computed matches.
Now produce final answer.
I'll write code in C++.
Potential micro-optimizations: Use unordered_map with custom hash for pair<int,int> might be faster. But map is simpler.
But careful: If using map with pair, it sorts by first then second, which is fine.
Alternatively, we can use a dictionary from int64 key: key = 1LL * rd * (n+1) + rk. But n varies per test, but we can use a large constant like 1e6+3 or 5e5+7 to avoid collisions. Since rd, rk <= n <= 5e5, we can use key = rd * 1000000LL + rk (since max n is 5e5, 1e6 > 5e5, safe). But using map with pair is simpler.
Thus code:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
map<pair<int, int>, int> cnt;
int d = 0, k = 0;
vector ans(n);
for (int i = 0; i < n; ++i) {
if (s[i] == 'D') ++d;
else ++k;
int g = __gcd(d, k);
int rd = d / g;
int rk = k / g;
ans[i] = ++cnt[{rd, rk}];
}
for (int i = 0; i < n; ++i) {
if (i) cout << ' ';
cout << ans[i];
}
cout << '\n';
}
return 0;
}
But note: Using __gcd may be in algorithm header; better to include <bits/stdc++.h> or . We'll include bits.
Now verify for d=0,k=0? Not possible.
Now confirm that for d=0, k>0: g = k, rd=0, rk=1. So key (0,1). Works. For d>0,k=0: g = d, rd=1, rk=0. Works.
Thus solution is correct.
Now I'll produce the final answer.
But before final, consider potential integer division issues: gcd(0,0) undefined; but not possible.
Now final answer: Provide explanation and code.
Now output. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
map<pair<int, int>, int> cnt;
int d = 0, k = 0;
vector<int> ans(n);
for (int i = 0; i < n; ++i) {
if (s[i] == 'D') ++d;
else ++k;
int g = gcd(d, k);
int rd = d / g;
int rk = k / g;
// increase the count for this reduced ratio and store the answer
ans[i] = ++cnt[{rd, rk}];
}
for (int i = 0; i < n; ++i) {
if (i) cout << ' ';
cout << ans[i];
}
cout << '\n';
}
return 0;
}