본문 바로가기

카테고리 없음

[BOJ]1181번: 단어 정렬

#include <iostream>
#include <algorithm>
#include <string>
 
int aPos, bPos;
 
bool compare(std::string a, std::string b) {
    if(a.length() != b.length()) {
        return a.length() < b.length();
    }
    else {
        aPos = -1, bPos = -1;
        while(aPos != a.length() - 1 || bPos != b.length() - 1) {
            if(aPos + 1 < a.length()) { aPos++; }
            if(bPos + 1 < b.length()) { bPos++; }
            if(a[aPos] != b[bPos]) {
                return a[aPos] < b[bPos];
            }
        }
        return false;
    }
}
 
int main(void) {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
 
    int len;
    std::cin >> len;
 
    std::string word[len];
    for(int idx = 0; idx < len; idx++) {
        std::cin >> word[idx];
    }
 
    std::sort(word, word + len, compare);
 
    for(int idx = 0; idx < len; idx++) {
        if(idx < len - 1 && word[idx] == word[idx + 1]) { continue; }
        std::cout << word[idx] << "\n";
    }
 
    return 0;
}
cs