fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool compare(string x1, string x2){
  4. if(x1.size() == x2.size()){
  5. return (x1 < x2);
  6. }
  7. else
  8. return (x1.size() < x2.size());
  9. }
  10. void check() {
  11. int p; cin>>p;
  12. vector<string>l(p);
  13. for(int i=0;i<p;i++) cin>>l[i];
  14. sort(l.begin(), l.end(), compare);
  15. for(auto e:l)
  16. cout<<e<<" ";
  17. cout<<"\n";
  18. }
  19. int main(){
  20. int t; cin>>t;
  21. while(t--){
  22. check();
  23. }
  24. return 0;
  25. }
Success #stdin #stdout 0.01s 5452KB
stdin
1
4
kiwi fig apple pear
stdout
fig kiwi pear apple