用最简单的排序做

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(string &a, string &b)
{
    return a.size() < b.size();
}

int main()
{
    string str;
    vector<string> a;
    int n;
    while (cin >> n)
    {
        getchar();
        for (int i = 0; i < n; ++i)
        {
            getline(cin, str);
            if (str == "stop")
                break;
            a.push_back(str);
        }
        sort(a.begin(), a.end(), cmp);
        for (auto itr = a.begin(); itr != a.end(); ++itr)
            cout << *itr << endl;
        a.clear();
    }
    return 0;
}