結果

問題 No.1192 半部分列
ユーザー pes_magicpes_magic
提出日時 2020-08-22 15:46:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,497 bytes
コンパイル時間 935 ms
コンパイル使用メモリ 75,552 KB
実行使用メモリ 818,176 KB
最終ジャッジ日時 2024-04-23 10:02:15
合計ジャッジ時間 2,671 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 MLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<vector<int>> getNext(const string& s){
    vector<vector<int>> res(s.size(), vector<int>(26, s.size()));
    vector<int> last(26, s.size()+1);
    for(int i=s.size()-1;i>=0;i--){
        last[s[i]-'a'] = i+1;
        for(int j=0;j<26;j++) res[i][j] = last[j];
    }
    return res;
}

string solve(const string& S, const string& T){
    auto nextS = getNext(S);
    auto nextT = getNext(T);
    vector<vector<int>> dp(S.size()+1, vector<int>(T.size()+1, 0));
    for(int i=S.size()-1;i>=0;i--){
        for(int j=T.size()-1;j>=0;j--){
            dp[i][j] = dp[i+1][j+1] + (S[i] == T[j] ? 1 : 0);
            dp[i][j] = max(dp[i][j], dp[i+1][j]);
            dp[i][j] = max(dp[i][j], dp[i][j+1]);
        }
    }
    if(dp[0][0] == S.size()) return "-1";
    int posS = 0, posT = 0;
    string res = "";
    while(posS < S.size()){
        for(int i=0;i<26;i++){
            int ns = nextS[posS][i];
            int nt = (posT < T.size() ? nextT[posT][i] : T.size()+1);
            if(ns > S.size()) continue;
            if(nt > T.size()){
                res += 'a'+i;
                return res;
            }
            if(dp[ns][nt] != S.size()-ns){
                res += 'a'+i;
                posS = ns;
                posT = nt;
                break;
            }
        }
    }
    return res;
}

int main(){
    string S, T;
    while(cin >> S >> T){
        cout << solve(S, T) << endl;
    }
}
0