結果

問題 No.205 マージして辞書順最小
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-19 08:11:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 963 bytes
コンパイル時間 843 ms
コンパイル使用メモリ 54,140 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-05 20:12:35
合計ジャッジ時間 1,505 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 12 ms
4,376 KB
testcase_08 AC 11 ms
4,376 KB
testcase_09 AC 12 ms
4,376 KB
testcase_10 AC 13 ms
4,376 KB
testcase_11 AC 12 ms
4,376 KB
testcase_12 AC 12 ms
4,376 KB
testcase_13 AC 8 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

const int kMAX_N = 100;

int N;
int start[kMAX_N];
string S[kMAX_N];

bool IsBetter(int index1, int index2) {
    string str1 = S[index1].substr(start[index1]),
           str2 = S[index2].substr(start[index2]);
    int len1 = str1.size(),
        len2 = str2.size();
    if (str1.substr(0, min(len1, len2)) == str2.substr(0, min(len1, len2))) {
        return len1 >= len2;
    } else {
        return str1 < str2;
    }
}

int main() {
    cin >> N;

    for (int i = 0; i < N; i++) {
        cin >> S[i];
    }

    string ans = "";
    while (true) {
        int index = -1;
        for (int i = 0; i < N; i++) {
            if (start[i] == S[i].size()) continue;
            if (index == -1 || IsBetter(i, index)) {
                index = i;
            }
        }
        if (index < 0) break;
        ans.push_back(S[index][start[index]]);
        start[index]++;
    }
    cout << ans << endl;

    return 0;
}
0