Problem F: STL——字符串排序

Problem F: STL——字符串排序

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 4429  Solved: 2307
[Submit][Status][Web Board]

Description

    对N个字符串排序。
    0<N<=50000。每个字符串长度不超过50000,所有字符串长度总和不超过1000000。

Input

    第一行读入N。
    后面N行,每行一个字符串(只包含字母)。

Output

    输出共N行,按字典序从小到大输出。

Sample Input

5
bcdef
qwer
tyuiphdjf
asdfghjklzzzz
z

  

Sample Output

asdfghjklzzzz
bcdef
qwer
tyuiphdjf
z

  

HINT

用STL的string容易解决。


Append Code

#include <iostream>
#include <set>
#include <string>
#include <functional>
using namespace std;
int main()
{
    multiset<string> str;.//multiset集合允许重复元素。
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        string arr;
        cin>>arr;
        str.insert(arr);
    }
    multiset<string> ::iterator p;
    for(p=str.begin(); p!=str.end(); p++)
        cout<<*p<<endl;

}

  

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>//sort() 函数的头文件
using namespace std;
int main()
{
    vector<string> str;
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        string arr;
        cin>>arr;
        str.push_back(arr);
    }
    sort(str.begin(), str.end());
    for(int i=0; i<n ;i++)
        cout<<str[i]<<endl;
}

  

作者:7oDo

仅供参考,请勿抄袭。

Hang Hang Hang !!!

原文地址:https://www.cnblogs.com/Jie-Fei/p/9128089.html