1536C.md (2707B)
1 Source: https://codeforces.com/problemset/problem/1536/C 2 3 C. Diluc and Kaeya 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 The tycoon of a winery empire in Mondstadt, unmatched in every possible way. A thinker in the Knights of Favonius with an exotic appearance. 22 23 This time, the brothers are dealing with a strange piece of wood marked with their names. This plank of wood can be represented as a string of $n$ characters. Each character is either a 'D' or a 'K'. You want to make some number of cuts (possibly $0$) on this string, partitioning it into several contiguous pieces, each with length at least $1$. Both brothers act with dignity, so they want to split the wood as evenly as possible. They want to know the maximum number of pieces you can split the wood into such that the ratios of the number of occurrences of 'D' to the number of occurrences of 'K' in each chunk are the same. 24 25 Kaeya, the curious thinker, is interested in the solution for multiple scenarios. He wants to know the answer for every prefix of the given string. Help him to solve this problem! 26 27 For a string we define a ratio as $a:b$ where 'D' appears in it $a$ times, and 'K' appears $b$ times. Note that $a$ or $b$ can equal $0$, but not both. Ratios $a:b$ and $c:d$ are considered equal if and only if $a\cdot d = b\cdot c$. 28 29 For example, for the string 'DDD' the ratio will be $3:0$, for 'DKD' — $2:1$, for 'DKK' — $1:2$, and for 'KKKKDD' — $2:4$. Note that the ratios of the latter two strings are equal to each other, but they are not equal to the ratios of the first two strings. 30 31 Input 32 33 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows. 34 35 The first line of each test case contains an integer $n$ ($1 \leq n \leq 5 \cdot 10^5$) — the length of the wood. 36 37 The second line of each test case contains a string $s$ of length $n$. Every character of $s$ will be either 'D' or 'K'. 38 39 It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$. 40 41 Output 42 43 For each test case, output $n$ space separated integers. The $i$-th of these numbers should equal the answer for the prefix $s_{1},s_{2},\dots,s_{i}$. 44 45 Example 46 47 Input 48 49 ``` 50 5 51 3 52 DDK 53 6 54 DDDDDD 55 4 56 DKDK 57 1 58 D 59 9 60 DKDKDDDDK 61 ``` 62 63 Output 64 65 ``` 66 1 2 1 67 1 2 3 4 5 6 68 1 1 1 2 69 1 70 1 1 1 2 1 2 1 1 3 71 ``` 72 73 Note 74 75 For the first test case, there is no way to partition 'D' or 'DDK' into more than one block with equal ratios of numbers of 'D' and 'K', while you can split 'DD' into 'D' and 'D'. 76 77 For the second test case, you can split each prefix of length $i$ into $i$ blocks 'D'.