题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805280074743808
题目描述
给定一个长度不超过 10^4 的、仅由英文字母构成的字符串。请将字符重新调整顺序,按 PATestPATest… 这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按 PATest 的顺序打印,直到所有字符都被输出。
输入
输入在一行中给出一个长度不超过 10^4 的、仅由英文字母构成的非空字符串。
输出
在一行中按题目要求输出排序后的字符串。题目保证输出非空。
样例输入
redlesPayBestPATTopTeePHPereatitAPPT
样例输出
PATestPATestPTetPTePePee
代码
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
map<char, int> m;
m['P'] = 0; m['A'] = 0; m['T'] = 0; m['e'] = 0; m['s'] = 0; m['t'] = 0;
char str[7] = {'P', 'A', 'T', 'e', 's', 't'};
bool flag = true;
string s1;
getline(cin, s1);
for(int i = 0; i < s1.size(); i++)
if(s1[i] == 'P' || s1[i] == 'A' || s1[i] == 'T' || s1[i] == 'e' || s1[i] == 's' || s1[i] == 't')
m[s1[i]]++;
while(flag) {
flag = false;
for(int i = 0; i < 6; i++) {
if(m[str[i]] > 0) {
cout << str[i];
m[str[i]]--;
flag = true;
}
}
}
cout<<endl;
return 0;
}