题目链接:Entertainment in MAC



思路

当当前操作次数n为偶数时,若原字符串大于反转字符串则可以将原字符串反转n - 2次,则得到的还是原字符串,此时反转一次,并将其再次反转的字符串加到反转字符串的末尾,此时得到词典最小的字符串,源字符串小于反转字符串时,直接将原字符串反转n次得到词典最小的字符串。

       当当前操作次数为奇数时,若原字符串大于反转字符串,则将字符串反转n次,得到原串的反转字符串,即为词典最小的字符串,若原字符串小于反转字符串,则反转n - 1次,再将

当前字符串反转加到当前字符串末尾得到词典最小的字符串。


代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10; void solve() {
ll n;
string s;
cin >> n >> s;
int l = 0, len = s.size();
while (s[l] == s[len - 1 - l] && l < len / 2) {
l++;
}
if (s[l] > s[len - l - 1]) {
if (n % 2) {
reverse(s.begin(), s.end());
}
else {
string auxious = s;
reverse(s.begin(), s.end());
s = s + auxious;
}
}
else if (s[l] < s[len - 1 - l]){
if (n % 2) {
string auxious = s;
reverse(s.begin(), s.end());
s = auxious + s;
}
}
cout << s << endl;
}
int main(){
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}

CodeForces 1935A的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

  10. CodeForces - 453A Little Pony and Expected Maximum

    http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...

随机推荐

  1. [FAQ] 快速准备 windows 的 nodejs 开发环境

      下载 git 版本控制系统:https://pc.qq.com/search.html#!keyword=git 下载 TortoiseGit 客户端:https://pc.qq.com/sear ...

  2. [PHP] 小数转科学计数法, 小数保留 n 位

    使用sprintf / printf 的 %e 或%E 格式说明符将其转换为科学计数法. 使用精度控制符指定保留多少位. 例如:sprintf('%.4e', 0.00000123); Link:ht ...

  3. GtkSharp 设置窗口背景透明

    本文告诉大家如何在 GTK Sharp 里面设置窗口背景透明 在 GTK 里面设置窗口背景透明十分简单,只需使用如下代码即可 this.AppPaintable = true; var screen ...

  4. 2.生产环境k8s-1.28.2集群小版本升级到1.28.5

    环境:https://www.cnblogs.com/yangmeichong/p/17956335 # 流程:先升级master,再升级node # 1.备份组件参考:https://kuberne ...

  5. netcore依赖注入通过反射简化

    aspnetcore里面用到许多的service,好多业务代码都要通过Service.AddScoped.Singleton.Transient等注入进去,类太多了写起来和管理起来都很麻烦,所以借鉴了 ...

  6. Lock、Monitor线程锁

    Lock.Monitor线程锁 官网使用 https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.monitor?view=net- ...

  7. 您可知道如何通过`HTTP2`实现TCP的内网穿透???

    可能有人很疑惑应用层 转发传输层?,为什么会有这样的需求啊???哈哈技术无所不用其极,由于一些场景下,对于一个服务器存在某一个内部网站中,但是对于这个服务器它没有访问外网的权限,虽然也可以申请端口访问 ...

  8. 应急响应--windows入侵排查

  9. NFS共享文件

    NFS共享文件 服务端 安装NFS [root@localhost www] yum -y install nfs-utils rpcbind 创建需要共享的文件夹share [root@localh ...

  10. nohup Command [ Arg … ] [ & ]

    nohup 英文全称:no hang up 不挂断的意思.退出终端不挂断程序的运行.在默认的情况下,会输出一个名叫 nohup.out 的文件到当前目录下.nohup Command [ Arg - ...