C. Diluc and Kaeya

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 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≤5105) — 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 5105.

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'.

线性DP

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        map<pair<int, int>, int> mp;
        int n;
        cin >> n;
        string str;
        cin >> str;
        int cou1 = 0, cou2 = 0;
        for (int i = 0; i < n; i++)
        {
            if (str[i] == 'D')
                cou1++;
            else
                cou2++;
            if (cou1 == 0 || cou2 == 0)
            {
                cout << i + 1 << " ";
            }
            else
            {
                int num = __gcd(cou1, cou2);
                int g1 = cou1 / num;
                int g2 = cou2 / num;
                mp[{g1, g2}]++;
                cout << mp[{g1, g2}] << " ";
            }
        }
        cout << endl;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Python 中实现一个原神抽奖模拟器,可以让用户模拟抽取原神中的角色或武器。 首先,需要定义好游戏中所有的角色和武器,以及它们的稀有度和抽取概率。可以将它们放在一个字典中,例如: ``` characters = { "Diluc": {"rarity": 5, "probability": 0.006}, "Jean": {"rarity": 5, "probability": 0.006}, "Keqing": {"rarity": 5, "probability": 0.006}, "Mona": {"rarity": 5, "probability": 0.006}, "Qiqi": {"rarity": 5, "probability": 0.006}, "Venti": {"rarity": 5, "probability": 0.006}, "Klee": {"rarity": 5, "probability": 0.006}, "Tartaglia": {"rarity": 5, "probability": 0.006}, "Zhongli": {"rarity": 5, "probability": 0.006}, "Albedo": {"rarity": 5, "probability": 0.006}, "Amber": {"rarity": 4, "probability": 0.051}, "Barbara": {"rarity": 4, "probability": 0.051}, "Beidou": {"rarity": 4, "probability": 0.051}, "Bennett": {"rarity": 4, "probability": 0.051}, "Chongyun": {"rarity": 4, "probability": 0.051}, "Diona": {"rarity": 4, "probability": 0.051}, "Fischl": {"rarity": 4, "probability": 0.051}, "Kaeya": {"rarity": 4, "probability": 0.051}, "Lisa": {"rarity": 4, "probability": 0.051}, "Ningguang": {"rarity": 4, "probability": 0.051}, "Noelle": {"rarity": 4, "probability": 0.051}, "Razor": {"rarity": 4, "probability": 0.051}, "Sucrose": {"rarity": 4, "probability": 0.051}, "Xiangling": {"rarity": 4, "probability": 0.051}, "Xingqiu": {"rarity": 4, "probability": 0.051}, "Yanfei": {"rarity": 4, "probability": 0.051}, "Rosaria": {"rarity": 4, "probability": 0.051}, "Sayu": {"rarity": 4, "probability": 0.051}, "Thoma": {"rarity": 4, "probability": 0.051}, "Traveler": {"rarity": 4, "probability": 0.051}, "Skyward Blade": {"rarity": 5, "probability": 0.007}, "Skyward Harp": {"rarity": 5, "probability": 0.007}, "Skyward Atlas": {"rarity": 5, "probability": 0.007}, "Skyward Spine": {"rarity": 5, "probability": 0.007}, "Skyward Pride": {"rarity": 5, "probability": 0.007}, "Lost Prayer to the Sacred Winds": {"rarity": 5, "probability": 0.007}, "Wolfs Gravestone": {"rarity": 5, "probability": 0.007}, "Vortex Vanquisher": {"rarity": 5, "probability": 0.007}, "Primordial Jade Winged-Spear": {"rarity": 5, "probability": 0.007}, "Aquila Favonia": {"rarity": 5, "probability": 0.007}, "Song of Broken Pines": {"rarity": 5, "probability": 0.007}, "Staff of Homa": {"rarity": 5, "probability": 0.007}, "Memory of Dust": {"rarity": 5, "probability": 0.007}, } ``` 然后,可以实现一个 `gacha` 函数来进行抽奖。该函数可以接受一个整数参数 `times`,表示抽奖次数。在函数内部,可以使用 Python 的随机数模块 `random` 来生成一个随机数,根据随机数和抽取概率来判断抽取到了哪个角色或武器。每次抽取后,可以将抽到的结果打印出来。 下面是一个简单的 `gacha` 函数的实现: ``` import random def gacha(times): for i in range(times): r = random.random() cumulative_probability = 0 for name, info in characters.items(): cumulative_probability += info["probability"] if r < cumulative_probability: print(f"{name} ({info['rarity']}★)") break ``` 最后,可以编写一个简单的主程序,让用户输入要抽取的次数,然后调用 `gacha` 函数进行抽奖。下面是一个示例: ``` if __name__ == "__main__": times = int(input("请输入要抽取的次数:")) gacha(times) ``` 这样,一个简单的原神抽奖模拟器就完成了。当然,这只是一个最基础的实现,你可以根据自己的需求和想法进行更复杂的开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值