CF1380B.Universal Solution

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string s=s1s2…sns = s_1 s_2 \dots s_{n} of length nn where each letter is either R, S or P.

While initializing, the bot is choosing a starting index pospos (1≤pos≤n1 \le pos \le n), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of sposs_{pos}:

  • if sposs_{pos} is equal to R the bot chooses "Rock";
  • if sposs_{pos} is equal to S the bot chooses "Scissors";
  • if sposs_{pos} is equal to P the bot chooses "Paper";

In the second round, the bot's choice is based on the value of spos+1s_{pos + 1}. In the third round — on spos+2s_{pos + 2} and so on. After sns_n the bot returns to s1s_1 and continues his game.

You plan to play nn rounds and you've already figured out the string ss but still don't know what is the starting index pospos. But since the bot's tactic is so boring, you've decided to find nn choices to each round to maximize the average number of wins.

In other words, let's suggest your choices are c1c2…cnc_1 c_2 \dots c_n and if the bot starts from index pospos then you'll win in win(pos)win(pos) rounds. Find c1c2…cnc_1 c_2 \dots c_n such that win(1)+win(2)+⋯+win(n)n\frac{win(1) + win(2) + \dots + win(n)}{n} is maximum possible.

最近,你发现了一个可以和你玩“石头剪刀布”的机器人。不幸的是,这个机器人采用了一种非常简单的算法:它拥有一串长度为 nn 的字符串 s=s1s2…sns = s_1 s_2 \dots s_{n},其中每个字符均为 R、S 或 P。

在初始化阶段,机器人会选定一个起始索引 pospos(满足 1≤pos≤n1 \le pos \le n),之后它可以进行任意轮数的游戏。在第 1 轮中,机器人根据 sposs_{pos} 的值选择“石头”、“剪刀”或“布”:

  • 若 spos=Rs_{pos} = \text{R},则机器人选择“石头”;
  • 若 spos=Ss_{pos} = \text{S},则机器人选择“剪刀”;
  • 若 spos=Ps_{pos} = \text{P},则机器人选择“布”。

在第 2 轮中,机器人的选择依据为 spos+1s_{pos + 1};在第 3 轮中,依据为 spos+2s_{pos + 2},依此类推。当到达 sns_n 后,机器人将循环回到 s1s_1 并继续游戏。

你计划进行 nn 轮游戏,且已知字符串 ss,但尚不清楚机器人的起始索引 pospos 是多少。然而,由于该机器人的策略过于单调乏味,你决定预先确定自己在每一轮中的出拳 c1c2…cnc_1 c_2 \dots c_n,以最大化获胜轮数的平均值。

换言之,假设你的出拳序列为 c1c2…cnc_1 c_2 \dots c_n,若机器人从索引 pospos 开始,则你在该情形下将赢得 win(pos)win(pos) 轮。请找出序列 c1c2…cnc_1 c_2 \dots c_n,使得 win(1)+win(2)+⋯+win(n)n\frac{win(1) + win(2) + \dots + win(n)}{n} 达到可能的最大值。

输入格式

The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

Next tt lines contain test cases — one per line. The first and only line of each test case contains string s=s1s2…sns = s_1 s_2 \dots s_{n} (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5; si∈R,S,Ps_i \in {\text{R}, \text{S}, \text{P}}) — the string of the bot.

It's guaranteed that the total length of all strings in one test doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 表示测试用例的数量。

接下来的 tt 行为测试用例,每行一个。每个测试用例仅有一行,包含一个字符串 s=s1s2…sns = s_1 s_2 \dots s_{n}(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5;si∈{R,S,P}s_i \in \{\text{R}, \text{S}, \text{P}\})—— 表示该机器人的出拳序列。

保证单次测试中所有字符串的总长度不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print nn choices c1c2…cnc_1 c_2 \dots c_n to maximize the average number of wins. Print them in the same manner as the string ss.

If there are multiple optimal answers, print any of them.

对于每个测试用例,输出 nn 个选择 c1c2…cnc_1 c_2 \dots c_n,以最大化获胜次数的平均值。输出格式需与字符串 ss 相同。

若存在多个最优解,输出任意一个即可。

输入输出样例

  • 输入#1

    3
    RRRR
    RSP
    S

    输出#1

    PPPP
    RSP
    R

说明/提示

In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all n=4n = 4 rounds, so the average is also equal to 44.

In the second test case:

  • if bot will start from pos=1pos = 1, then (s1,c1)(s_1, c_1) is draw, (s2,c2)(s_2, c_2) is draw and (s3,c3)(s_3, c_3) is draw, so win(1)=0win(1) = 0;
  • if bot will start from pos=2pos = 2, then (s2,c1)(s_2, c_1) is win, (s3,c2)(s_3, c_2) is win and (s1,c3)(s_1, c_3) is win, so win(2)=3win(2) = 3;
  • if bot will start from pos=3pos = 3, then (s3,c1)(s_3, c_1) is lose, (s1,c2)(s_1, c_2) is lose and (s2,c3)(s_2, c_3) is lose, so win(3)=0win(3) = 0;

The average is equal to 0+3+03=1\frac{0 + 3 + 0}{3} = 1 and it can be proven that it's the maximum possible average.

A picture from Wikipedia explaining "Rock paper scissors" game:

在第一个测试用例中,机器人(无论从何处开始)总会选择“石头”,因此我们总可以选择“布”。于是,在任何情况下,我们都将赢得全部 n=4n = 4 轮,故平均值也为 44。

在第二个测试用例中:

  • 若机器人从 pos=1pos = 1 开始,则 (s1,c1)(s_1, c_1) 为平局,(s2,c2)(s_2, c_2) 为平局,(s3,c3)(s_3, c_3) 为平局,因此 win(1)=0win(1) = 0;
  • 若机器人从 pos=2pos = 2 开始,则 (s2,c1)(s_2, c_1) 为获胜,(s3,c2)(s_3, c_2) 为获胜,(s1,c3)(s_1, c_3) 为获胜,因此 win(2)=3win(2) = 3;
  • 若机器人从 pos=3pos = 3 开始,则 (s3,c1)(s_3, c_1) 为失败,(s1,c2)(s_1, c_2) 为失败,(s2,c3)(s_2, c_3) 为失败,因此 win(3)=0win(3) = 0;

平均值为 0+3+03=1\frac{0 + 3 + 0}{3} = 1,且可以证明这是可能达到的最大平均值。

维基百科中解释“石头剪刀布”游戏的一张图片:

输入解题思路,AI测评打分。不知道怎么写?

首页