結果

問題 No.205 マージして辞書順最小
ユーザー togatogatogatoga
提出日時 2015-08-24 23:25:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,339 bytes
コンパイル時間 3,783 ms
コンパイル使用メモリ 151,280 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 16:46:24
合計ジャッジ時間 2,668 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define mp       make_pair
#define mt	 make_tuple
#define pb       push_back
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    pair<int,int>      pii;
typedef    pair<long,long>    pll;

const int INF=1<<29;
const double EPS=1e-9;
const int MOD = 100000007;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int N;
vector<string> S;
bool compare(const string &tmp1, const string &tmp2, int pos){
  if (pos >= tmp1.size()){
    return false;
  }
  if (pos >= tmp2.size()){
    return true;
  }
  if (tmp1[pos] == tmp2[pos]){
    return compare(tmp1, tmp2, pos + 1);
  }
  return tmp1[pos] < tmp2[pos];
}
int calc(){
  string tmp = S[0];
  int index = 0;
  for (int i = 1; i < S.size(); i++){
    if (compare(S[i], tmp, 0)){
      tmp = S[i];
      index = i;
    }
  }
  return index;
}

int main(){
  cin >> N;
  S.resize(N);
  for (int i = 0; i < N; i++){
    cin >> S[i];
  }
  string res = "";
  while (true){
    int index = calc();
    if (S[index].size() == 1){
      res += S[index][0];
      S.erase(S.begin() + index);
    }else{
      res += S[index][0];
      string tmp = S[index].substr(1);
      S.erase(S.begin() + index);
      S.push_back(tmp);
    }
    if (S.size() == 0)break;
  }
  cout << res << endl;
}
0