Huawei Written Questions: Find the longest path of a string

 

Title description

Given n strings, please arrange the n strings in lexicographic order.

Enter description:

Enter the first line as a positive integer n (1≤n≤1000), the following n lines are n strings (string length≤100), the string contains only upper and lower case letters

Output description:

The data is output in n lines, and the output result is a character string arranged in lexicographic order.

Example 1

Input

9
cap
to
cat
card
two
too
up
boat
boot

Output

boat
boot
cap
card
cat
to
too
two
up

 method one:

#include <iostream>
#include <string>
#include <set>
using namespace std;


int main() {
    int n;
    cin >> n;
    multiset<string> se;
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        se.insert(s);
    }
    multiset<string>::iterator ptr;
    for (ptr = se.begin(); ptr!=se.end(); ptr++) {
        cout << *ptr << endl;
    }
    return 0;
}

Method Two:

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


int main() {
    int n;
    cin >> n;
    vector<string> ve;
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        ve.push_back(s);
    }
    sort(ve.begin(),ve.end());
    for (int j = 0; j < n; ++j) {
        cout << ve[j] << endl;
    }
    return 0;
}

 

Published 34 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/weixin_41111088/article/details/104791362