Codeforces-学校排队-程序员宅基地

技术标签: 算法  c++  codeforces 每日一题  数据结构  

B. Queue at the School

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

During the break the schoolchildren, boys and girls, formed a queue of n people in the canteen. Initially the children stood in the order they entered the canteen. However, after a while the boys started feeling awkward for standing in front of the girls in the queue and they started letting the girls move forward each second.

Let's describe the process more precisely. Let's say that the positions in the queue are sequentially numbered by integers from 1 to n, at that the person in the position number 1 is served first. Then, if at time x a boy stands on the i-th position and a girl stands on the (i + 1)-th position, then at time x + 1 the i-th position will have a girl and the (i + 1)-th position will have a boy. The time is given in seconds.

You've got the initial position of the children, at the initial moment of time. Determine the way the queue is going to look after t seconds.

Input

The first line contains two integers n and t (1 ≤ n, t ≤ 50), which represent the number of children in the queue and the time after which the queue will transform into the arrangement you need to find.

The next line contains string s, which represents the schoolchildren's initial arrangement. If the i-th position in the queue contains a boy, then the i-th character of string s equals "B", otherwise the i-th character equals "G".

Output

Print string a, which describes the arrangement after t seconds. If the i-th position has a boy after the needed time, then the i-th character a must equal "B", otherwise it must equal "G".

 

译文

学校排队

时间限制:2 秒

内存限制:256 MB

输入:标准输入

输出:标准输出

课间休息时,男女学生在食堂排起了 n 人的长队。起初,他们按照进入食堂的顺序站好。然而,过了一会儿,男生开始觉得站在女生前面很尴尬,于是他们开始让女生每秒钟向前移动一次。

让我们更精确地描述一下这个过程。假设队列中的位置按 1 到 n 的整数顺序编号,1 号位置上的人先得到服务。那么,如果在第 x 秒时,第 i 个位置上站着一个男孩,第 (i + 1) 个位置上站着一个女孩,那么在第 x + 1 秒时,第 i 个位置上将站着一个女孩,第 (i + 1) 个位置上将站着一个男孩。

你已经得到了学生们在初始时刻的初始位置。确定 t 秒后队列的样子。

输入

第一行包含两个整数 n 和 t(1 ≤ n,t ≤ 50),分别表示队列中的人数和队列转变为所需排列的时间。

下一行包含字符串 s,它代表学生的初始排列。如果队列中的第 i 个位置是男孩,则字符串 s 的对应第 i 个字符等于“B”,否则第 i 个字符等于“G”。

输出

打印字符串 a,表示 t 秒后队列的样子。如果第 i 个位置在 t 秒后是一个男孩,则第 i 个字符必须为 "B",否则为 "G"。

 

样例


输入

5 1

BGGBG

输出

GBGGB


输入

5 2

BGGBG

输出

GGBGB


输入

4 1

GGGB

输出

GGGB


 

题解

#include <bits/stdc++.h>
using namespace std;
int n, t;
string s;
int main()
{
    cin >> n >> t >> s;
    // 需要同时将当前这一秒的所有BG全部改为GB
    while (t--)
    {
        for (int i = 0; i < s.size();)
        {
            if (s[i] == 'B' && s[i + 1] == 'G')
                swap(s[i], s[i + 1]), i += 2;
            else
                i += 1;
        }
    }
    cout << s;
    return 0;
}

 

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/NUJIEW/article/details/135686824

智能推荐

2020 年中国程序员薪资和生活现状调查报告_2020年薪酬数据报告-程序员宅基地

文章浏览阅读1.7k次,点赞6次,收藏3次。作者 | 程序员客栈来源 | ID:proginnwx根据中国互联网络信息中心(CNNIC)近日发布第 44 次《中国互联网络发展状况统计报告》。截至 2019 年 06 月,中国网民规模为 8.54 亿,较 2018 年底增加 2598 万。网上外卖用户规模达 4.21 亿,较 2018 年底增长 1516 万;网络视频用户规模达 7.59 亿,较 2018 年底增长 3391 万;我..._2020年薪酬数据报告

从 NASL 说开:低代码编程语言能饭否_网易nasl语言-程序员宅基地

文章浏览阅读1.3w次,点赞78次,收藏73次。轻舟低代码平台,NASL是其中的关键,也是最大的差异化。回头看Gartner的定义,也有LCAP(低代码平台)和CADP(无代码平台)之分,前者对开发完整性、应用独立性、逻辑完备性、可接入可集成等都有要求,所以说,NASL的实现使得轻舟低代码更符合这一理念。说到框架,大家首先想到肯定是是react,springboot这种被广泛使用的前后端框架,低代码框架本质上跟他们类似,差别在于,低代码框架通常支持从前后端到数据库的全栈模型实现,而且封装程度更高。_网易nasl语言

数字音频总线A2B开发详解三(A2B系统的Flash Program详解)_a2b技术学习-程序员宅基地

文章浏览阅读1.2k次。A2B系统的EEPROM自启动如何做,本文来告诉你_a2b技术学习

Code Blocks 安装后编译出现“编译器无效”问题。_codeblocks编译器无效-程序员宅基地

文章浏览阅读2.2k次。编写程序并编译后出现如上图报错,大致意思为:编译器安装无效,所以编译器无法运行编译器,请检查编译器路径有没有正确配置,并且给出了个修改步骤,最后说设置的路径没有找到编译器文件。。。解决方法:S1:首先我们要确定我们下载安装Code Blocks是带有编译器版本的,就是带有MinGW的版本:下载地址:http://www.codeblocks.org/downloads/26S2:然后给软件指定MinGW的所在路径:菜单栏Settings -> Compil..._codeblocks编译器无效

神操作!一行Python代码搞定一款游戏?给力!-程序员宅基地

文章浏览阅读475次。点击上方“Python大本营”,选择“置顶公众号”Python大本营 IT人的职业提升平台来源:pypl编程榜一直以来Python长期霸占编程语言排行榜前三位,其简洁,功能强大的特性..._python好游戏代码

ubuntu 1604 random: crng init done 后无反应_random: fast init done-程序员宅基地

文章浏览阅读2.4k次。服务器安装Ubuntu1604报错报错内容:kernel panic - not syncing attempted to kill the idle taskrandom: fast init donerandom: crng init done错误原因:使用了错误的安装镜像,CPU位宽64位使用了i386的安装镜像,切换amd64安装镜像后无报错..._random: fast init done

随便推点

php 上传文件漏洞,PHP -- 文件包含、文件上传漏洞-程序员宅基地

文章浏览阅读330次。PHP -- 文件包含、文件上传漏洞PHP -- 文件包含、文件上传漏洞文件包含文件引入漏洞,是由一个动态页面编译时引入另一个文件的操作。文件引入本身是没有问题,它是用于统一页面风格,减少代码冗余的一种技术。但是在特定的场景下就会形成漏洞jsp:include指令和include动作,其中include动作用于引入动态文件php:include(),include_once(),require()..._php文件上传4漏洞

配置NGINX同时运行 https 和 http_nginx 和 http无法同时启动-程序员宅基地

文章浏览阅读406次。SSL 是需要申请证书的,key和PEM文件要放到服务器路径。然后NGINX下要进行443端口和80端口的绑定。server { listen 80; server_name ietaiji.com www.ietaiji.com; root "D:/aaa/WWW/ietaiji"; index index.html_nginx 和 http无法同时启动

总结:linux之Service_linux service-程序员宅基地

文章浏览阅读1.3w次,点赞9次,收藏60次。service与systemctl关系梳理开机启动梳理_linux service

揭开数据中心光模块利润之源-程序员宅基地

文章浏览阅读194次。在数据中心里,光模块毫不起眼,当我们在高谈阔论各种数据中心高大上技术时,很少提及到光模块。不过,光模块却是数据中心的必需品,哪个数据中心也离不开光模块,而且需要的数量还不少,一块48端口网络设备就需要48个光模块,而一台框式网络设备通常有数百个端口,这些端口如果都使用上就需要数百个光模块,这样算起来数据中心需要的光模块数量是惊人的。数据中心在进行网络投资..._光模块 占 数据中心 成本

Java NIO SocketChannel简述及示例_niosocketchannel-程序员宅基地

文章浏览阅读596次,点赞2次,收藏3次。SocketChannel简述及demoJAVA NIO之SocketChannel1. 简述2. 特点3. 解决问题4. demo功能5. 工作原理6. 代码示例7. 涉及知识扩充JAVA NIO之SocketChannel1. 简述NIO(Non-blocking I/O,在Java领域,也称为New I/O),是一种同步非阻塞的I/O模型,也是I/O多路复用的基础,已经被越来..._niosocketchannel

docker内的debian9使用ntpdate同步时间时报错step-systime: Operation not permitted-程序员宅基地

文章浏览阅读3.8k次。场景在docker下使用tzselect设置时间,最后提示编辑profile追加TZ='Asia/Shanghai'; export TZvim/etc/profilesource /etc/profile发现时区正确了,但时间和宿主机相差几分钟....使用 ntpdate cn.pool.ntp.org同步时间提示ntpdate[534]: step-systime: Operation not permitted使用 ntpdate ntp.s..._step-systime: operation not permitted