본문 바로가기

알고리즘

[Algorithm] 문자열 정렬

문제


n개의 문자열이 주어질 때, 이 문자열을 사전순으로 빠른 순서대로 정렬하는 프로그램을 작성하시오.  

입력


첫 번째 줄에 문자열의 개수 n이 주어진다 ( 1 ≤ n ≤ 100 ) 그 후 n개의 줄에 대하여 정렬하고자 하는 문자열이 주어진다 ( 1 ≤ 문자열의 길이 ≤ 100 )  

출력


문자열을 사전순으로 빠른 순서대로 정렬한 결과를 출력한다.

 

예제 입력

9
acid
apple
banana
acquire
cat
crop
crab
power
cat

예제 출력

acid
acquire
apple
banana
cat
cat
crab
crop
power


<코드>


#include<iostream>

#include<vector>

#include<algorithm>


using namespace std;


int main(){

  vector<string> a;

  int n;

  cin>>n;

  

  for(int i=0; i<n; i++){

    char word[100];

    cin>>word;

    a.push_back(word);

  }

  sort(a.begin(),a.end());

  

  for(int i=0; i<n; i++){

    cout<<a[i]<<endl;

  }


}


'알고리즘' 카테고리의 다른 글

[Algorithm] 문자열 압축  (0) 2019.02.04
[Algorithm] 팰린드롬 조사  (0) 2019.02.03
[Algorithm] 문자열 뒤집기  (0) 2019.02.03
[Algorithm] 과제물 망치기  (0) 2019.02.03
[Algorithm] 대소문자변환  (0) 2019.02.02