첫주차라 난이도가 어렵지 않았고 c++ STL sort와 정렬 기준에 맞는 compare함수를 별도로 구현하여 문제를 해결했다.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int n, k;
bool compare(string a, string b) {
if (a.length() == b.length()) return a < b;
return a.length() < b.length();
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> k;
vector<string> words(n);
for (int i = 0; i < n; i++) cin >> words[i];
sort(words.begin(), words.end(), compare);
cout << words[k-1];
return 0;
}
'Alrogithm > 구름(GOORM)' 카테고리의 다른 글
[구름 알고리즘 먼데이 챌린지] Week1 : 4. 소수 찾기 (0) | 2022.11.04 |
---|---|
[구름 알고리즘 먼데이 챌린지] Week1 : 3. 최장 맨해튼 거리 (0) | 2022.11.04 |
[구름 알고리즘 먼데이 챌린지] Week1 : 2. 동명이인 (0) | 2022.11.04 |
[구름 알고리즘 먼데이 챌린지] Week1 : 1. 경로의 개수 (0) | 2022.11.04 |
[구름 알고리즘 먼데이 챌린지] Week0 : 2. 카드 교환하기 (2) | 2022.11.03 |