Codeforces Round #724 (Div. 2) C. Diluc and Kaeya题解

题目如下:

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

5
3
DDK
6
DDDDDD
4
DKDK
1
D
9
DKDKDDDDK

output

1 2 1 
1 2 3 4 5 6 
1 1 1 2 
1 
1 1 1 2 1 2 1 1 3 

大致意思就是让你求每个给定字符串的前缀子串最多能分成多少个D与K的比例都一样的段

拿到题目的时候我是懵的,搁哪疯狂想怎么贪心,结果题没做出来,头发少了几根,无奈去看了题解,wow,这简直了。

这题是数形结合的做法,我们建立一个平面直角坐标系,假设我们在原点,定义D是右移一位,K是上移一位,那么对应点的与原点连线的斜率就代表了该点的前缀子串的DK比例,当我们不是第一次遇见这个斜率时,我们就可以知道当前目标串前面有多少个和它比率一样的子串了。

ac代码

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
map<double,int>x;

int main(){
	int t;
	cin>>t;
	while(t--){
		x.clear();
		int n;
		cin>>n;
		string a;
		cin>>a;
		int numx=0,numy=0;
		for(int i=0;i<n;i++){
			if(a[i]=='D'){
				numx++;
			}else{
				numy++;
			}
			if(numx==0){
				if(x[INF]==0){//特判一下斜率无穷大的情况
					cout<<1<<" ";
					x[INF]++;
				}else{
					cout<<x[INF]+1<<" ";
					x[INF]++;
				}
			}else{
				double k=(double)numy/numx;
				if(x[k]==0){
					cout<<1<<" ";
					x[k]++;
				}else{
					cout<<x[k]+1<<" ";
					x[k]++;
				}
			}			
		}
		cout<<endl;	
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_58178876/article/details/120500619