CodeForces 1935A Entertainment in MAC
1.CodeForces 1992E Novice's Mistake
2.CodeForces 1935A Entertainment in MAC
3.CodeForces 1992C Gorilla and Permutation4.CodeForces 1992D Test of Love5.CodeForces 1992B Angry Monk6.CodeForces 1992A Only Pluses7.CodeForces 1983B Corner Twist8.CodeForces 1983A Array Divisibility9.CodeForces 1983C Have Your Cake and Eat It Too10.CodeForces 1883B Chemistry11.CodeForces 1883A Morning12.CodeForces 1883C Raspberries13.CodeForces 1883D In Love14.CodeForces 1883E Look Back15.CodeForces 1883F You Are So Beautiful16.CodeForces 1883G1 Dances (Easy version)17.CodeForces 908B New Year and Buggy Bot18.CodeForces 908C New Year and Curling19.CodeForces 1132B Discounts20.CodeForces 1619D New Year's Problem21.Codeforces Round 964 (Div. 4)题目链接: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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】