C. Diluc and Kaeya
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The tycoon of a winery empire in Mondstadt, unmatched in every possible way. A thinker in the Knights of Favonius with an exotic appearance.
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 nn characters. Each character is either a 'D' or a 'K'. You want to make some number of cuts (possibly 00) on this string, partitioning it into several contiguous pieces, each with length at least 11. 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.
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!
For a string we define a ratio as a:ba:b where 'D' appears in it aa times, and 'K' appears bb times. Note that aa or bb can equal 00, but not both. Ratios a:ba:b and c:dc:d are considered equal if and only if a⋅d=b⋅ca⋅d=b⋅c.
For example, for the string 'DDD' the ratio will be 3:03:0, for 'DKD' — 2:12:1, for 'DKK' — 1:21:2, and for 'KKKKDD' — 2:42: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.
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001≤t≤1000). Description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤5⋅1051≤n≤5⋅105) — the length of the wood.
The second line of each test case contains a string ss of length nn. Every character of ss will be either 'D' or 'K'.
It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055⋅105.
Output
For each test case, output nn space separated integers. The ii-th of these numbers should equal the answer for the prefix s1,s2,…,sis1,s2,…,si.
Example
input
Copy
5 3 DDK 6 DDDDDD 4 DKDK 1 D 9 DKDKDDDDK
output
Copy
1 2 1 1 2 3 4 5 6 1 1 1 2 1 1 1 1 2 1 2 1 1 3
Note
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'.
For the second test case, you can split each prefix of length ii into ii blocks 'D'.
题意有个关键地方,那就是要求当前能分成比例相同的不同段,且段是连续的,既然每段比例相同,那么它们都应该等于当前长度的DF比例,比如要分成2:1若干段,当前长度里面,cntd:cntf一定等于2:1.我们每一个长度都统计一下比例,并把比例次数+1,然后直接输出次数即可。why?
首先这几段的比例必须是当前比例无疑,然后如果这个比例包含本次共出现两次,设第一次出现位置len1 ,这一段比例确定无疑,然而len1到当前段比例也可以求得,必须两段比例相同才能构成当前比例。正好次数等于段数。
#include<iostream>
# include<cstring>
# include<algorithm>
# include<math.h>
# include<cmath>
# include<map>
using namespace std;
typedef long long int ll;
map<double,int>m;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
double cnt1=0,cnt2=0;
int ans=0;
for(int i=0;i<s.length();i++)
{
if(s[i]=='D')
cnt1++;
else
cnt2++;
double q=cnt1/cnt2;
m[q]++;
cout<<m[q]<<" ";
}
m.clear();
cout<<'\n';
}
}