728x90
320x100
1181번: 단어 정렬
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
www.acmicpc.net
반응형
📌 접근 방법
- 길이가 짧은 순서로 정렬하되, 길이가 같으면 사전순으로 정렬
- 중복은 제거한다
➡ vector로 입력받아서 정렬기준을 정해서 sort() 했다.
➡ 반복문을 통해 중복을 제거했다.
✅ Pass Code
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
bool compare(string a, string b){
if(a.length()==b.length()){
return a<b;
}
else{
return a.length()<b.length();
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin>>N;
string arr[20000];
for(int i=0; i<N; i++){
cin>>arr[i];
}
sort(arr, arr+N, compare);
for(int i=0; i<N; i++){
if(arr[i]==arr[i-1])continue; //중복제거
else cout<<arr[i]<<'\n';
}
return 0;
}
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/043.gif)
728x90
반응형
'Coding Test > Baekjoon' 카테고리의 다른 글
[BOJ/백준/C++] 18870번 좌표 압축 (0) | 2024.01.27 |
---|---|
[BOJ/백준/C++] 1269번 대칭 차집합 (0) | 2024.01.27 |
[BOJ/백준/C++] 1436번 영화감독 숌 (1) | 2024.01.26 |
[BOJ/백준/C++] 2231번 분해합 (1) | 2024.01.26 |
[BOJ/백준/C++] 1018번 체스판 다시 칠하기 (1) | 2024.01.26 |