題目
給定一個字符串s,s包括以空格分隔的若干個單詞,請對s進行如下處理后輸出:
1、單詞內部調整:對每個單詞字母重新按字典序排序
2、單詞間順序調整:
1)統計每個單詞出現的次數,并按次數降序排列
2)次數相同,按單詞長度升序排列
3)次數和單詞長度均相同,按字典升序排列
請輸出處理后的字符串,每個單詞以一個空格分隔。
輸入描述:
一行字符串,每個字符取值范圍:【a-zA-z0-9】以及空格,字符串長度范圍:【1,1000】
例1:
輸入
This is an apple
輸出
an is This aelpp
例2:
輸入:
My sister is in the house not in the yard
輸出:
in in eht eht My is not adry ehosu eirsst
#include <cstdio>
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <map>
#include <vector>using namespace std;bool cmp(pair<string, int> it1, pair<string, int> it2){if(it1.second == it2.second){if(it1.first.length() == it2.first.length()){return it1.first < it2.first;}else{return it1.first.length() < it2.first.length();}}else{return it1.second > it2.second;}
}int main(){string word;map<string, int> all_cnt;while(cin >> word){sort(word.begin(), word.end());all_cnt[word]+= 1;word.clear();}vector<pair<string, int> > vecWords(all_cnt.begin(), all_cnt.end());sort(vecWords.begin(), vecWords.end(), cmp);bool first = true;int n = vecWords.size();for(int i = 0; i < n;i++){pair<string, int> use_pair = vecWords[i];for(int j = 0; j < use_pair.second;j++){if(first){first = false;cout << use_pair.first;}else{cout << " " << use_pair.first;}}}cout << endl;
}