結果

問題 No.205 マージして辞書順最小
ユーザー EmKjpEmKjp
提出日時 2015-03-25 01:22:58
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2,632 ms / 5,000 ms
コード長 1,495 bytes
コンパイル時間 780 ms
コンパイル使用メモリ 87,440 KB
実行使用メモリ 245,376 KB
最終ジャッジ日時 2024-06-29 00:45:18
合計ジャッジ時間 20,538 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

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)){
        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