레벨: 실버 5
날짜: 2023년 4월 9일 오후 7:42
문제번호: 1181
알고리즘: 문자열, 정렬
키워드: 구현, 정렬
시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 | 256 MB | 136970 | 57236 | 42748 | 40.361% |
알파벳 소문자로 이루어진 N개의 단어가 들어오면 아래와 같은 조건에 따라 정렬하는 프로그램을 작성하시오.
단, 중복된 단어는 하나만 남기고 제거해야 한다.
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
조건에 따라 정렬하여 단어들을 출력한다.
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(string a, string b);
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
string A[n];
for(int i=0; i<n; i++) {
cin>>A[i];
}
sort(A, A + n, compare); //compare에 따라 정렬 솔직히 좀 어려움
string before;
for(int i=0; i<n; i++) {
if(before == A[i]) //중복 단어 제거
continue;
cout<<A[i]<<'\n';
before = A[i];
}
}
bool compare(string a, string b){
if(a.length() != b.length()) { //길이 다르면 긴거가 뒤로
return a.length() < b.length();
}
else //길이 같으면 사전순
return a < b;
}