本文共 1194 字,大约阅读时间需要 3 分钟。
输入样例: redlesPayBestPATTopTeePHPereatitAPPT 输出样例: PATestPATestPTetPTePePee
输入样例:
思路:建立一张表,字符为关键字,对应字符出现的次数为关键字对应的值。输出“PATest”的时候查表即可。
#include #include #include #include #include #include #include using namespace std;void print(const string& s);int main(){ string s; cin>>s; print(s); return 0;}void print(const string& s){ int* p=new int[128]; memset(p,0,sizeof(int)*128); for(string::const_iterator itor=s.begin(); itor!=s.end() ; itor++) p[(int)(*itor)]++; while(p[(int)'P'] && p[(int)'A'] && p[(int)'T'] && p[(int)'e'] && p[(int)'s'] && p[(int)'t']) { cout<<"PATest"; p[(int)'P']--;p[(int)'A']--;p[(int)'T']--;p[(int)'e']--;p[(int)'s']--;p[(int)'t']--; } while(p[(int)'P'] || p[(int)'A'] || p[(int)'T'] || p[(int)'e'] || p[(int)'s'] || p[(int)'t']) { if(p[(int)'P']!=0) { cout<<'P'; p[(int)'P']--; } if(p[(int)'A']!=0) { cout<<'A'; p[(int)'A']--; } if(p[(int)'T']!=0) { cout<<'T'; p[(int)'T']--; } if(p[(int)'e']!=0) { cout<<'e'; p[(int)'e']--; } if(p[(int)'s']!=0) { cout<<'s'; p[(int)'s']--; } if(p[(int)'t']!=0) { cout<<'t'; p[(int)'t']--; } } delete p;}
转载于:https://www.cnblogs.com/FDProcess/p/9241259.html