반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

코딩하기 좋은날

백준 1181 단어 정렬 본문

백준(Baekjoon) 문제

백준 1181 단어 정렬

huiung 2019. 1. 20. 00:36
반응형
문제와 채점은 위 사이트에서 확인 하실 수 있습니다.

 

이 문제는 단어들을 입력받고 먼저 길이 순으로 정렬을 하고 같은 길이는 사전순으로 정렬을 해서 출력을 해야합니다.

 

저는 그래서 우선은 multimap에 길이와 문자열을 입력받아 길이 순으로 정렬을 하고 길이가 같은 문자열들을 set에 넣어준뒤 출력 하는 방법으로 풀었습니다.

 

다른 사람들 풀이를 보니 정렬 조건을 넣은 함수를 만들어서 간단하게 푼것을 보니 좀 허무했습니다 ㅠㅠ..

 

제가푼 코드입니다.

 

#include <iostream>
#include <string>
#include <map> 
#include <set>

using namespace std;

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	string str;
	multimap<int , string> mm;
	set<string> s;

	int N;
	cin >> N;
	while(N) { //multimap에 문자열 길이 순으로 저장 
		cin >> str;
		int len = str.length();
		mm.insert(make_pair(len, str));
		N--;
	}
	
	for(auto it = mm.begin(); it != mm.end(); it++) {
		auto it2 = it;
		it2++;
		if(it->first == it2->first) { //길이가 동일할 때 사전순으로 출력 
			s.insert(it->second);
			while(it->first == it2->first) {
				s.insert(it2->second);
				it++;
				it2++;
			}
			
			for(auto n: s)
				cout<<n<<'\n';
			s.clear();
			continue;
		}
		else
			cout<<it->second<<'\n';
	}
	
	return 0;
}
반응형

'백준(Baekjoon) 문제' 카테고리의 다른 글

백준 1205 등수 구하기  (0) 2019.01.20
백준 10825 국영수  (0) 2019.01.20
백준 1026 보물  (0) 2019.01.19
백준 4949 균형잡힌 세상  (0) 2019.01.19
백준 9935 문자열 폭발  (0) 2019.01.19