게으른카르

백준 1181 단어 정렬 C++

C
2023. 4. 7. 00:35

백준 1181 단어 정렬.

백준 1181번 "단어 정렬" 문제의 자세한 내용은 글 하단의 문제 링크를 참고하세요.

1181번 문제에 주어지는 입력 및 예시

입력: 

13
but
i
wont
hesitate
no
more
no
more
it
cannot
wait
im
yours

출력:

i
im
it
no
but
more
wait
wont
yours
cannot
hesitate

코드

백준 1181번 "단어 정렬" 문제의 코드입니다.

#include <bits/stdc++.h>
using namespace std;
vector<string> a;
bool comp(string a,string b){
	if(a.length()==b.length()){
		return a<b;
	}
	else{
		return a.length()<b.length();
	} 
}
int main(void) {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
    	string tmp;
		cin>>tmp;
    	a.push_back(tmp);
	}
	sort(a.begin(),a.end(),comp);
	a.erase(unique(a.begin(),a.end()),a.end()); //중복제거 
	for(auto v: a){
		cout<<v<<"\n";
	}
    return 0;
}

실행

위의 코드를 예제의 입력을 넣어 실행했을 때의 결과입니다.

반응형

'C' 카테고리의 다른 글

백준 2583 영역 구하기 C++  (0) 2023.04.10
백준 2468 안전 영역 C++  (0) 2023.04.10
백준 1920 수 찾기 C++  (0) 2023.04.07
백준 1260 DFS와 BFS C++  (0) 2023.04.06
백준 1874 스택 수열 C++  (0) 2023.04.06

이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band