結果

問題 No.205 マージして辞書順最小
ユーザー EmKjpEmKjp
提出日時 2015-03-25 01:26:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,618 ms / 5,000 ms
コード長 1,725 bytes
コンパイル時間 749 ms
コンパイル使用メモリ 89,224 KB
実行使用メモリ 373,820 KB
最終ジャッジ日時 2023-09-11 10:02:15
合計ジャッジ時間 13,735 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,972 KB
testcase_01 AC 4 ms
7,740 KB
testcase_02 AC 34 ms
24,060 KB
testcase_03 AC 198 ms
48,996 KB
testcase_04 AC 83 ms
38,552 KB
testcase_05 AC 113 ms
40,548 KB
testcase_06 AC 4 ms
7,732 KB
testcase_07 AC 2,403 ms
373,820 KB
testcase_08 AC 283 ms
163,312 KB
testcase_09 AC 1,800 ms
353,104 KB
testcase_10 AC 2,618 ms
245,012 KB
testcase_11 AC 1,407 ms
205,256 KB
testcase_12 AC 935 ms
200,420 KB
testcase_13 AC 1,928 ms
309,388 KB
testcase_14 AC 5 ms
8,028 KB
testcase_15 AC 4 ms
7,692 KB
testcase_16 AC 4 ms
7,688 KB
testcase_17 AC 4 ms
7,692 KB
testcase_18 AC 4 ms
7,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<numeric>
#include<functional>
#include<complex>
#include<cassert>

using namespace std;
#define BET(a,b,c) ((a)<=(b)&&(b)<(c))
#define FOR(i,n) for(int i=0,i##_end=(int(n));i<i##_end;i++)
#define SZ(x) (int)(x.size())
#define ALL(x) (x).begin(),(x).end()
#define MP make_pair
#define FOR_EACH(it,v) for(__typeof(v.begin()) it=v.begin(),it_end=v.end() ; it != it_end ; it++)
typedef vector<int> VI;
typedef vector<VI> VVI;

string memo[2510][55];

string dfs(const string&s1, const string&s2, int p1, int p2){
    string& ret = memo[p1][p2];
    if(ret != "") return ret;
    if(p1 < SZ(s1) && p2 < SZ(s2)){
        if(s1[p1] < s2[p2]){
            ret = s1[p1] + dfs(s1, s2, p1+1, p2);
        }
        if(s1[p1] > s2[p2]){
            ret = s2[p2] + dfs(s1, s2, p1, p2+1);
        }
        if(s1[p1] == s2[p2]){
            string a = s1[p1] + dfs(s1, s2, p1+1, p2);
            string b = s2[p2] + dfs(s1, s2, p1, p2+1);
            ret = min(a, b);
        }
    }else if(p1 < SZ(s1)){
        ret = s1[p1] + dfs(s1, s2, p1+1, p2);
    }else if(p2 < SZ(s2)){
        ret = s2[p2] + dfs(s1, s2, p1, p2+1);
    }
    return ret;
}

string merge(string s1, string s2){
    FOR(i,SZ(s1)+1) FOR(j,SZ(s2)+1) {
        memo[i][j] = "";
    }
    return dfs(s1, s2, 0, 0);
}

int main()
{
    int n;
    cin>>n;
    assert(1 <= n && n <= 50);
    vector<string> s(n);
    FOR(i,n) {
        cin>>s[i];
    }
    string ans = s[0];
    for(int i=1;i<n;i++) ans = merge(ans, s[i]);
    cout<<ans<<endl;
    return 0;
}
0