fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. map <string, int> mp;
  8. vector <string> v;
  9. int n, m;
  10.  
  11.  
  12. bool isMore(string a, string b){
  13. if(mp[a] > mp[b]){
  14. return true;
  15. }
  16. else{
  17. return false;
  18. }
  19. }
  20.  
  21.  
  22. bool isLonger(string a, string b){
  23. if(a.length() > b.length()){
  24. return true;
  25. }
  26. else{
  27. return false;
  28. }
  29. }
  30.  
  31. int main(){
  32.  
  33. cin.tie(NULL);
  34. cout.tie(NULL);
  35.  
  36. cin >> n >> m;
  37.  
  38. for(int i = 0; i < n; i++){
  39.  
  40. string s;
  41.  
  42. cin >> s;
  43.  
  44. if(s.length() < m){
  45. continue;
  46. }
  47.  
  48. v.push_back(s);
  49.  
  50. if(mp.find(s) == mp.end()){
  51. mp[s] = 1;
  52. }
  53. else{
  54. mp[s]++;
  55. }
  56. }
  57.  
  58. if(v.empty()){
  59. return 0;
  60. }
  61.  
  62. // 사전순으로 배열
  63. sort(v.begin(), v.end());
  64.  
  65. // 길이가 긴것 앞으로 배치
  66. sort(v.begin(), v.end(), isLonger);
  67.  
  68. // 자주 나오는 단어 앞에 배치
  69. sort(v.begin(), v.end(), isMore);
  70.  
  71. //중복 제거
  72. v.erase(unique(v.begin(), v.end()), v.end());
  73.  
  74. for(int i = 0; i < v.size(); i++){
  75. cout << v[i] << "\n";
  76. }
  77.  
  78.  
  79.  
  80. }
Success #stdin #stdout 0.01s 5300KB
stdin
17 1
aa
bb
cc
dd
ee
ff
gg
hh
a
b
c
d
e
f
g
h
i
stdout
i
b
c
d
e
f
g
h
a
aa
ff
ee
gg
dd
cc
hh
bb