PAT 乙级 1043 输出PATest

输入样例:
redlesPayBestPATTopTeePHPereatitAPPT
输出样例:
PATestPATestPTetPTePePee

思路:建立一张表,字符为关键字,对应字符出现的次数为关键字对应的值。输出“PATest”的时候查表即可。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <algorithm>

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;
}

猜你喜欢

转载自www.cnblogs.com/FDProcess/p/9241259.html