일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- level1
- trie
- binary search
- 프로세스 상태
- SWEA
- BFS
- Zenject
- Bronze
- Euclidean
- 3D RPG
- stack
- LEVEL2
- 8-Puzzle
- solid 원칙
- 프로그래머스
- Silver
- BOJ
- Flyweight Pattern
- Project
- Gold
- programmers
- algorithm
- knapsack Problem
- level3
- Unity
- Modern C++
- two pointer
- PrefixSum
- dirtyflag pattern
- effective C++
Archives
- Today
- Total
Patrick's Devlog
[BOJ/C++] 단어 정렬(1181번) 본문
1. 개요
https://www.acmicpc.net/problem/1181
1181번: 단어 정렬
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
www.acmicpc.net
1-1. 설명
알파벳 소문자로 이루어진 N개의 단어가 들어오면 아래와 같은 조건에 따라 정렬하는 프로그램을 작성한다.
1. 길이가 짧은 것
2. 길이가 같으면 사전순
1-2. 제한 사항
- 첫 줄에 단어의 개수 N이 주어지며, N은 1 이상 20000이하 자연수
- 둘째 줄부터 N개 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한줄에 하나씩 주어지며, 문자열 길이는 50을 넘지 않음
2. 구현
2-1. 풀이
sort 함수를 이용해 정렬 조건을 지정하여 정렬하였다. 중복은 출력할 때 제거해주었다.
2-2. 코드
#include <iostream>
#include <algorithm>
using namespace std;
constexpr size_t MAX_NUM = 20000;
string words[MAX_NUM];
bool compare(string a, string b);
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
string preStr;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> words[i];
}
sort(words, words + N, compare);
for (int i = 0; i < N; i++) {
if (i == 0) preStr = words[i];
else if (preStr == words[i]) continue;
preStr = words[i];
cout << words[i] << "\n";
}
return 0;
}
bool compare(string a, string b) {
if (a.size() < b.size()) return true;
else if (a.size() == b.size()) return a < b;
else return false;
}
'Algorithm > Algorithms Practice' 카테고리의 다른 글
[BOJ/C++] 숫자 카드 2(10816번) (0) | 2022.09.09 |
---|---|
[BOJ/C++] 좌표 정렬하기(11650번) (0) | 2022.09.08 |
[BOJ/C++] 덱(10866번) (0) | 2022.09.06 |
[BOJ/C++] 큐(10845번) (0) | 2022.09.02 |
[BOJ/C++] 스택(10828번) (0) | 2022.09.02 |