NC222152. CocktailWithSwap
描述
输入描述
In the first line, input three integer, indicating , the meaning is shown in the statement.
The next line input a string of length .Represents the initial string.
输出描述
Output a string representing the smallest lexicographical string reached after several operations with follow the rules as statement describe.
示例1
输入:
5 1 1 edcba
输出:
abcde
示例2
输入:
5 4 4 edcba
输出:
adcbe
C++ 解法, 执行用时: 12ms, 内存消耗: 912K, 提交时间: 2021-05-22 17:23:37
#include <bits/stdc++.h> using namespace std; int main(){ string str, ans; int n, l, r; cin >> n >> l >> r; cin >> str; if(l == r){ string tmp[l]; for(int i = 0; i < n; i++){ tmp[i%l] += str[i]; } for(int i = 0; i < l; i++) sort(tmp[i].begin(), tmp[i].end()); for(int i = 0; i <= n / l; i++){ for(int j = 0; j < l; j++){ if(i >= tmp[j].size()) continue; ans += tmp[j][i]; } } cout << ans << endl; }else if(n >= l * 2){ sort(str.begin(), str.end()); cout << str << endl; }else{ string tmp; for(int i = 0; i < n; i++){ if(i < n - l || i >= n - (n - l)) tmp += str[i]; } sort(tmp.begin(), tmp.end()); int cnt = 0; for(int i = 0; i < n; i++){ if(i < n - l || i >= n - (n - l)) str[i] = tmp[cnt++]; } cout << str << endl; } }