알고리즘 · 코딩
[SWEA 7701] 염라대왕의 이름 정렬
G A
2020. 12. 28. 01:16
문제 링크
swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWqU0zh6rssDFARG
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
C++ 풀이
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(string A, string B) {
if (A.length() < B.length())
return true;
else if (A.length() == B.length())
return A < B;
else if (A.length() > B.length())
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T, N, pos;
string name;
vector<string> names;
cin >> T;
for (int i = 1; i <= T; i++) {
cin >> N;
names.resize(N);
for (int j = 0; j < N; j++)
cin >> names[j];
sort(names.begin(), names.end(), cmp);
cout << "#" << i << "\n";
cout << names[0] << "\n";
for (int m = 1; m < names.size(); m++) {
if (names[m] != names[m - 1]) //중복 문자열은 제외하고 출력
cout << names[m] << "\n";
}
}
return 0;
}
중복 문자열은 제외하고 출력하는 방식으로 풀었다.
후에 unique 함수를 통해 중복 원소를 제거하는 방법이 있다는 걸 배웠다.
반응형