๐ PS/Algorithm
[Baekjoon] 1181๋ฒ: ๋จ์ด ์ ๋ ฌ
hades1
2024. 8. 4. 12:58
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1181
๐ ์ค๊ณ
1์ฐจ๋ก ๋จ์ด ๊ธธ์ด ์์ผ๋ก ์ ๋ ฌ, 2์ฐจ๋ก ์ฌ์ ์์ผ๋ก ์ ๋ ฌํ๊ณ , ์ค๋ณต์ ์ ๊ฑฐํ๋ฉด ๋๋ค.
์ค๋ณต์ ์ ๊ฑฐํ๊ธฐ ์ํด unique์ erase๋ฅผ ์ฌ์ฉํ๋ค. unique๋ ์ค๋ณต์ด ์๋ ์์๋ค์ ๋ฒกํฐ์ ๋์ผ๋ก ๋ณด๋ด๊ณ , ์ค๋ณต ์์๋ค์ ์์์ ์ ๋ฐํํ๋ค. erase๋ erase(iterator first, iterator last); ์์ [first, last) ๋ฒ์์ ์์๋ฅผ ์ ๊ฑฐํ๋ค.
์ค๋ณต์ ์ ๊ฑฐํ์ผ๋ฏ๋ก, ๋จ์์๋ ์์์ ๊ฐ์๋ n์ด ์๋ ์ ์์ผ๋ฏ๋ก ์ฃผ์ํ๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n;
string s;
vector<string> words;
bool compare(string a, string b) {
if (a.length() < b.length()) {
return true;
}
else if (a.length() == b.length()) {
return a < b;
}
else {
return false;
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
words.push_back(s);
}
sort(words.begin(), words.end(), compare);
words.erase(unique(words.begin(), words.end()), words.end());
for (int i = 0; i < words.size(); i++) {
cout << words[i] << "\n";
}
return 0;
}