Algorithm Problems/BOJ
[1181번] 단어 정렬 - Sorting[C++]
개발자인생
2017. 3. 27. 21:36
https://www.acmicpc.net/problem/1186
간단히 정렬하는 문제로 좀 특이한 점이 있다면 단어 길이 sorting를 신경써야한다는 점이다.
처음 든 생각은 length 와 string을 구조체에 넣고 계산할까 하다가
string 타입과 sort함수를 사용하면 코드가 간결해질 것 같은 느낌적인 느낌이 들어서 이들을 이용하였다.
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 32 33 | #include <iostream> #include <string> #include <string.h> #include <algorithm> using namespace std; bool compare(const string& a, const string& b){ if(a.length() == b.length()) return (a < b); else return a.length() < b.length(); } int main() { int N; string input[20001]; cin >> N; for(int i = 0; i < N; i++) cin >> input[i]; sort(input, input + N, compare); for(int i = 0; i < N; i++){ if(input[i] == input[i-1] && i != 0) continue; cout << input[i] << endl; } return 0; } | cs |