输入一串字母例如:redlesPayBestPATTOPTeePHPereatitAPPT。筛选这串字母中的字母使其拼凑出尽量多的PATest序列,并且欠缺字母时可忽略。例如这里:
其实该算法就是利用散列表用于计数每个字母的次数。
#include<string>
#include <iostream>
using namespace std;
int main()
{
string s;
cin>>s;
string dic = "PATest";
int count[200] = {};
for (int i = 0; i < s.size(); i++)
{
count[(int)s[i]]++;//字母转为ACSII
}
int flag = 0;
while (true)
{
for (int i = 0; i < dic.size(); i++)
{
if (count[(int)dic[i]] != 0) {
count[(int)dic[i]]--;
cout << dic[i];
flag = 1;
}
}
if (flag == 0) break;
}
}