题目描述
给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按
GPLTGPLT....
这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按GPLT
的顺序打印,直到所有字符都被输出。输入格式:
输入在一行中给出一个长度不超过10000的、仅由英文字母构成的非空字符串。
输出格式:
在一行中按题目要求输出排序后的字符串。题目保证输出非空。
思路
存储'G' 'P' 'L' 'T' 的数量, 轮流使用直到都用光为止
代码
#include <bits/stdc++.h>
#define PII pair<int, int>
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 500005;
using namespace std;
map<char, int> cnt;
string s;
int main(){
cin >> s;
for(auto c : s) cnt[toupper(c)]++;
while(cnt['G'] > 0 || cnt['P'] > 0 || cnt['L'] > 0 || cnt['T'] > 0){
if(cnt['G']-- > 0) cout << 'G';
if(cnt['P']-- > 0) cout << 'P';
if(cnt['L']-- > 0) cout << 'L';
if(cnt['T']-- > 0) cout << 'T';
}
return 0;
}