fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int cmp(const string& a, const string& b)
  8. {
  9. if (a.length() == b.length()) //길이가 같다면
  10. return a < b; //사전순으로 올림차순
  11. else
  12. return a.length() < b.length(); //길이순으로 올림차순
  13. }
  14.  
  15. int main(void)
  16. {
  17. ios::sync_with_stdio(false);
  18. cin.tie(NULL);
  19.  
  20. int n;
  21. cin >> n;
  22.  
  23. string* word = new string[n];
  24.  
  25. for (int i = 0; i < n; i++)
  26. cin >> word[i];
  27.  
  28. sort(word, word + n, cmp);
  29.  
  30. for (int i = 0; i < n; i++)
  31. {
  32. cout << word[i] << '\n';
  33. while (word[i] == word[i + 1]) i++; //현재 문장과 다음 문자이 같다면 다음 문장으로 접근(중복 방지)
  34. }
  35.  
  36. delete[] word;
  37. return 0;
  38. }
Success #stdin #stdout 0s 4672KB
stdin
13
but
i
wont
hesitate
no
more
no
more
it
cannot
wait
im
yours
stdout
i
im
it
no
but
more
wait
wont
yours
cannot
hesitate