結果

問題 No.205 マージして辞書順最小
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-08-16 21:37:19
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 1,349 bytes
コンパイル時間 1,843 ms
コンパイル使用メモリ 172,128 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-07 18:22:27
合計ジャッジ時間 2,926 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N;
    vector<string> S;
    void input() {
        cin >> N;
        S.resize(N); cin >> S;
    }

    bool comp(string s, string t) {
        for (int i = 0; i < min(s.size(), t.size()); i++) {
            if (s[i] == t[i]) continue;
            return s[i] < t[i];
        }
        return s.size() > t.size();
    }

    void solve() {
        string T;
        while (true) {
            if (S.empty()) break;
            sort(S.begin(), S.end(), comp);
            T.push_back(S[0][0]);
            S[0] = S[0].substr(1, S[0].size() - 1);
            if (S[0].empty()) {
                vector<string> t;
                for (int i = 1; i < S.size(); i++) t.push_back(S[i]);
                S = t;
            }
        }

        cout << T << endl;
    }
}

int main() {
    input(); solve();
    return 0;
}

0