๐Ÿ‘Š 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;
}