끄적끄적 코딩
article thumbnail

정렬 문제입니다.

sort함수로 글자가 짧은것 순으로 정렬하고
글자 길이가 같으면 글자순으로 정렬합니다.

그리고 erase, unique를 써서 중복을 제거한 후
차례대로 출력했습니다.

 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
using namespace std;

bool compare(string a, string b) {
	if (a.size() == b.size()) {
		return a < b;
	}
	else {
		return a.size() < b.size();
	}
}

int main(int argc, char *argv[])
{
	int n;
	char s[100];
	vector<string> v;

	scanf("%d", &n);

	for (int i = 0; i < n; ++i) {
		scanf("%s", s);
		v.push_back(s);
	}

	sort(v.begin(), v.end(), compare);
	v.erase(unique(v.begin(), v.end()), v.end());

	for (int i = 0; i < v.size(); ++i) {
		cout << v[i] << endl;
	}

	return 0;
}

검색 태그