欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1744C.Traffic Light
Traffic Light
You find yourself on an unusual crossroad with a weird traffic light. That traffic light has three possible colors: red (r), yellow (y), green (g). It is known that the traffic light repeats its colors every seconds and at the -th second the color is on.
That way, the order of the colors is described by a string. For example, if "rggry", then the traffic light works as the following: red-green-green-red-yellow-red-green-green-red-yellow- ... and so on.
More formally, you are given a string of length . At the first second the color is on, at the second — , ..., at the -th second the color is on, at the -st second the color is on and so on.
You need to cross the road and that can only be done when the green color is on.
You know which color is on the traffic light at the moment, but you don't know the current moment of time. You need to find the minimum amount of time in which you are guaranteed to cross the road.
You can assume that you cross the road immediately.
For example, with "rggry" and the current color r there are two options: either the green color will be on after second, or after . That way, the answer is equal to — that is the number of seconds that we are guaranteed to cross the road, if the current color is r.
Input
The first line contains a single integer ) — the number of test cases.
Then the description of the test cases follows.
The first line of each test case contains an integer and a symbol (, is one of allowed traffic light colors r, y or g)— the length of the string and the current color of the traffic light.
The second line of each test case contains a string of the length , consisting of the letters r, y and g.
It is guaranteed that the symbol g is in the string and the symbol is in the string .
It is guaranteed, that the sum of over all test cases does not exceed .
Output
For each test case output the minimal number of second in which you are guaranteed to cross the road.
Note
The first test case is explained in the statement.
In the second test case the green color is on so you can cross the road immediately.
In the third test case, if the red color was on at the second second, then we would wait for the green color for one second, and if the red light was on at the first second, then we would wait for the green light for two seconds.
In the fourth test case the longest we would wait for the green color is if we wait for it starting from the fifth second.
Samples
6
5 r
rggry
1 g
g
3 r
rrg
5 y
yrrgy
7 r
rgrgyrg
9 y
rrrgyyygy
3
0
2
4
1
4
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |