CF #724(div2)C. Diluc and Kaeya,暴力,构造
Posted 小哈里
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF #724(div2)C. Diluc and Kaeya,暴力,构造相关的知识,希望对你有一定的参考价值。
problem
C. Diluc and Kaeya
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.
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: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⋅d=b⋅c.
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.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). Description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤5⋅105) — the length of the wood.
The second line of each test case contains a string s of length n. Every character of s will be either ‘D’ or ‘K’.
It is guaranteed that the sum of n over all test cases does not exceed 5⋅105.
Output
For each test case, output n space separated integers. The i-th of these numbers should equal the answer for the prefix s1,s2,…,si.
Example
inputCopy
5
3
DDK
6
DDDDDD
4
DKDK
1
D
9
DKDKDDDDK
outputCopy
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 i into i blocks ‘D’.
solution
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e7+10;
LL gcd(LL a, LL b){ return b==0 ? a : gcd(b,a%b); }
int f[maxn];
int main(){
int T; cin>>T;
while(T--){
int n; string s; cin>>n>>s;
LL d = 0, k = 0;
map<pair<LL,LL>, LL>ma;
for(int i = 0; i < s.size(); i++){
if(s[i]=='D')d++;
else k++;
int dd=0, kk = 0, gc=gcd(d,k);
if(d==0 || k==0){
if(d>0)dd=1;else kk=1;
}else{
dd = d/gc, kk = k/gc;
}
if(ma.count({dd,kk})){
f[i] = f[ma[{dd,kk}]-1]+1;
}else{
f[i] = 1;
}
ma[{dd,kk}] = i+1;
}
for(int i = 0; i < n; i++)
cout<<f[i]<<" ";
cout<<"\\n";
}
return 0;
}
以上是关于CF #724(div2)C. Diluc and Kaeya,暴力,构造的主要内容,如果未能解决你的问题,请参考以下文章
C. Diluc and Kaeya——Codeforces Round #724 (Div. 2)
Codeforces Round #724 (Div. 2) C. Diluc and Kaeya(map暴力)
CF #724(div2)A. Omkar and Bad Story,贪心,构造序列
CF #738(div2)C. Mocha and Hiking(构造)