• 思维难度：普及/提高-
• 实现难度：普及/提高-
• 推荐指数：5.0 （推荐指数最高为10.0）

## 题目翻译

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 $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 $ad=bc$ .

## 题解

hash表的版本 $O(n)$，平衡树的版本 $O(n\log n)$

（使用 std::map 的版本可以去洛谷看其他人的题解，因为有别人写了我就换一个）

#include<stdio.h>
#include<string.h>
#define MAXN 500003
struct hash_node {
int x,y;
int sum;
int next;
}h[MAXN];
int tot;
int hashcode(int x,int y)
{
return (x*1331+y)%MAXN;
}
{
int code=hashcode(x,y),p;
{
if(h[p].x==x&&h[p].y==y)
{
h[p].sum++;
return h[p].sum;
}
}
return 1;
}
int gcd(int x,int y)
{
int r=x%y;
while(r)
{
x=y;
y=r;
r=x%y;
}
return y;
}
int main()
{
int T,i,n,dsum,ksum,x,y,g;
char c;
scanf("%d",&T);
while(T--)
{
tot=dsum=ksum=0;
memset(h,0,sizeof(h));

scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf(" %c",&c);
if(c=='D') dsum++;
else ksum++;
if(dsum==0) x=0,y=1;
else if(ksum==0) x=1,y=0;
else
{
g=gcd(dsum,ksum);
x=dsum/g,y=ksum/g;
}
printf("%d ",g);
}
printf("\n");
}
return 0;
}

## 发送评论编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
（╯‵□′）╯︵┴─┴
￣﹃￣
(/ω＼)
∠( ᐛ 」∠)＿
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ｀)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ(￣∇￣o)
ヾ(´･ ･｀｡)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò｡)
Σ(っ °Д °;)っ
( ,,´･ω･)ﾉ"(´っω･｀｡)
╮(╯▽╰)╭
o(*////▽////*)q
＞﹏＜
( ๑´•ω•) "(ㆆᴗㆆ)

Emoji