1043 输出PATest(20 分)
给定一个长度不超过 104 的、仅由英文字母构成的字符串。请将字符重新调整顺序,按 PATestPATest....
这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按 PATest 的顺序打印,直到所有字符都被输出。
输入格式:
输入在一行中给出一个长度不超过 104 的、仅由英文字母构成的非空字符串。
输出格式:
在一行中按题目要求输出排序后的字符串。题目保证输出非空。
输入样例:
redlesPayBestPATTopTeePHPereatitAPPT
输出样例:
PATestPATestPTetPTePePee
分析:通过map记录所输入字符串中P、A、T、e、s、t几个字符的个数,同时记录字符的总数。输出时,当对应字符存在时便输出,同时此字符map值减一,直到总数为零为止。
代码:
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<char, int> m;
int main() {
string s;
cin >> s;
int cnt = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'P' || s[i] == 'A' || s[i] == 'T' ||
s[i] == 'e' || s[i] == 's' || s[i] == 't') {
m[s[i]]++;
cnt++;
}
}
while (cnt != 0) {
if (m['P'] != 0) {
cout << 'P';
m['P']--;
cnt--;
}
if (m['A'] != 0) {
cout << 'A';
m['A']--;
cnt--;
}
if (m['T'] != 0) {
cout << 'T';
m['T']--;
cnt--;
}
if (m['e'] != 0) {
cout << 'e';
m['e']--;
cnt--;
}
if (m['s'] != 0) {
cout << 's';
m['s']--;
cnt--;
}
if (m['t'] != 0) {
cout << 't';
m['t']--;
cnt--;
}
}
return 0;
}