題目連結:Entertainment in MAC
思路
程式碼
#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;
}