1181번: 단어 정렬
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
www.acmicpc.net
[ 백준 1181번 - 단어 정렬 소스 코드 ]
#include <iostream>
#include <vector>
#include <algorithm>
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()
{
vector <string> a;
string temp= "";
int N;
cin >> N;
for(int i=0;i<N;i++)
{
string str;
cin >> str;
a.push_back(str);
}
sort(a.begin(),a.end(),compare);
for(int i=0;i<N;i++)
{
if(temp==a[i])continue;
cout << a[i] << '\n';
temp=a[i];
}
return 0;
}
'백준 문제풀이' 카테고리의 다른 글
백준 2505 - 두 번 뒤집기 (0) | 2021.05.01 |
---|---|
백준 2567번 - 색종이2 소스 코드 (0) | 2021.04.18 |
백준 1546번 - 평균 소스 코드 (0) | 2021.04.18 |
백준 10825번 - 국영수 소스 코드 (0) | 2021.04.18 |
백준 2504번 - 괄호의 값 (0) | 2021.04.16 |