输入样例:
redlesPayBestPATTopTeePHPereatitAPPT
输出样例:
PATestPATestPTetPTePePee
代码1: 将P,A,T,e,s,t分别存入数组再输出
#include <iostream>
#include <vector>
#include<string>
using namespace std;
int main() {
string str;
cin >> str;
vector<char>P, A, T, e, s, t;
for (int i = 0;i < str.size();i++) {
if (str[i] == 'P') {
P.push_back(str[i]);
}
if (str[i] == 'A') {
A.push_back(str[i]);
}
if (str[i] == 'T') {
T.push_back(str[i]);
}
if (str[i] == 'e') {
e.push_back(str[i]);
}
if (str[i] == 's') {
s.push_back(str[i]);
}
if (str[i] == 't') {
t.push_back(str[i]);
}
}
int p=0,a=0,x=0,E=0,S=0,y=0;
while (true) {
if (P.size() != 0) {
cout << P[p];
P.erase(P.begin() + p);
p--;
}
if (A.size() != 0) {
cout << A[a];
A.erase(A.begin() + a);
a--;
}
if (T.size() != 0) {
cout << T[x];
T.erase(T.begin() + x);
x--;
}
if (e.size() != 0) {
cout << e[E];
e.erase(e.begin() + E);
E--;
}
if (s.size() != 0) {
cout << s[S];
s.erase(s.begin() + S);
S--;
}
if (t.size() != 0) {
cout << t[y];
t.erase(t.begin() + y);
y--;
}
p++;a++;x++;E++;S++;y++;
if (P.size() == 0 && A.size() == 0 && T.size() == 0 && e.size() == 0 && s.size() == 0 && t.size() == 0) {
break;
}
}
return 0;
}
代码2: 直接把字母作为数组下标
#include <iostream>
#include<string>
using namespace std;
int main() {
string str;
cin >> str;
int a[200] = { 0 };
int i;
for (i = 0;i < str.size();i++) {
a[str[i]]++;
}
while (a['P'] > 0|| a['A'] > 0|| a['T'] > 0|| a['e'] > 0|| a['s'] > 0|| a['t'] > 0)
{
if (a['P'] > 0) { cout << "P"; a['P']--; }
if (a['A'] > 0) { cout << "A"; a['A']--; }
if (a['T'] > 0) { cout << "T"; a['T']--; }
if (a['e'] > 0) { cout << "e"; a['e']--; }
if (a['s'] > 0) { cout << "s"; a['s']--; }
if (a['t'] > 0) { cout << "t"; a['t']--; }
}
return 0;
}