BOJ - 1181 - 단어정렬

ggh·2022년 5월 11일
0

백준

목록 보기
6/112

BOJ - 1181 - 단어정렬

문제


SOL


  • 문자열의 길이순으로 정렬 후 사전순으로 정렬
    • sort의 compare를 사용하여 조건문을 만들어 줌.
      bool compare(string a, string b)
      {
          if(a.size() == b.size())
              return a < b;
          return a.size() < b.size(); 
      }
  • 중복 제거

    st_list.erase(unique(st_list.begin(), st_list.end()), st_list.end());

정답 소스


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

bool compare(string a,
             string b)
{
    if(a.size() == b.size())
        return a < b;
    return a.size() < b.size(); 
}

int main()
{
    int num;
    string str;
    vector<string> st_list;
    scanf("%d", &num);
    for(int i=0; i < num; i++)
    {
        cin >> str;
        st_list.push_back(str);
    }
    sort(st_list.begin(), st_list.end(), compare);
    st_list.erase(unique(st_list.begin(), st_list.end()), st_list.end());
    for(int i=0; i < st_list.size(); i++)
        cout << st_list[i] << endl;
}

출력


13
but
i
wont
hesitate
no
more
no
more
it
cannot
wait
im
yours
------------
i
im
it
no
but
more
wait
wont
yours
cannot
hesitate

Reference & 다른 PS 모음집


C++ STL sort ( )

https://github.com/ggh-png/PS

profile
See you in a minute. : )

0개의 댓글