1181번: 단어 정렬
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
www.acmicpc.net
배운것
벡터 중복제거
sort(v.begin(), v.end(), cmp);
a.erase(unique(v.begin(), v.end()), v.end());
정답 코드
using namespace std;
bool cmp(string a, string b)
{
int alen = a.size();
int blen = b.size();
if (alen != blen)return alen < blen;
return a < b;
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
string str;
cin >> n;
vector<string> a;
for (int i = 0; i < n; i++)
{
cin >> str;
a.push_back(str);
}
sort(a.begin(), a.end(), cmp);
a.erase(unique(a.begin(), a.end()), a.end());
for (int i = 0; i < a.size(); i++)
cout << a[i] << '\n';
return 0;
}
'Algorithm > C++' 카테고리의 다른 글
[백준 11656] 접미사 배열 (0) | 2022.10.04 |
---|---|
[백준 10814] 나이순 정렬 (0) | 2022.10.04 |
[백준 15663] N과 M(9) (0) | 2022.09.28 |
[백준 15657] N과 M (8) (0) | 2022.09.28 |
[백준 9095] 1,2,3 더하기 (0) | 2022.09.28 |
댓글