https://www.acmicpc.net/problem/1181
1181번: 단어 정렬
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
www.acmicpc.net
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;
int N;
vector<string> ans;
set<string> s;
// set 컨테이너를 이용해서 set에 key를 삽입하면 중복을 알아서 처리해줌
// set 컨테이너에서 정렬을 써도 될것 같지만 그냥 내가 아는 방식인 vector에 옮긴 후 정렬 처리해줌
// 정렬 함수
bool func(string& s1, string& s2)
{
// 제일 먼저 길이 순으로 정렬
if (s1.size() < s2.size())
{
return true;
}
else if(s1.size() > s2.size())
{
return false;
}
// 길이가 같다면 사전 순 처리
else
{
if (s1 < s2)
return true;
else
return false;
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N;
while (N--)
{
string tmp;
cin >> tmp;
s.insert(tmp);
}
for (auto it = s.begin(); it != s.end(); ++it)
{
ans.push_back(*it);
}
int a = 3;
sort(ans.begin(), ans.end(), func);
for (auto it = ans.begin(); it != ans.end(); ++it)
{
cout << *it << '\n';
}
return 0;
}
map과 비슷한 set 컨테이너를 이용해서 중복 제외 처리 후 정렬 함수 써서 푼 문제
'알고리즘 문제 > C++' 카테고리의 다른 글
[C++] 백준 1874번: 스택 수열 (0) | 2022.08.31 |
---|---|
[C++] 백준 11866번: 요세푸스 문제 0 (0) | 2022.08.30 |
알고리즘 문제 풀다가 발견한 STL 함수 (0) | 2022.08.22 |
[C++] 백준 8979번 : 올림픽(부분 성공) (0) | 2022.05.22 |
[C++] 백준 2822번 : 점수 계산 (0) | 2022.05.20 |