結果

問題 No.1192 半部分列
ユーザー pes_magicpes_magic
提出日時 2020-08-22 16:00:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 1,352 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 74,728 KB
実行使用メモリ 30,592 KB
最終ジャッジ日時 2024-04-23 10:14:42
合計ジャッジ時間 1,937 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 43 ms
30,592 KB
testcase_07 AC 38 ms
30,336 KB
testcase_08 AC 37 ms
30,336 KB
testcase_09 AC 35 ms
29,440 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 38 ms
30,336 KB
testcase_13 AC 16 ms
15,488 KB
testcase_14 AC 22 ms
20,608 KB
testcase_15 AC 21 ms
19,456 KB
testcase_16 AC 15 ms
14,976 KB
testcase_17 AC 21 ms
19,712 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 7 ms
8,064 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 18 ms
17,152 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 37 ms
30,464 KB
testcase_24 AC 7 ms
8,192 KB
testcase_25 AC 15 ms
14,848 KB
testcase_26 AC 16 ms
14,976 KB
testcase_27 AC 16 ms
16,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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<int> dp(T.size()+1, S.size());
    for(int i=T.size()-1;i>=0;i--){
        dp[i] = dp[i+1];
        if(dp[i] == 0) continue;
        if(S[dp[i]-1] == T[i]) --dp[i];
    }
    if(dp[0] == 0) 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[nt] > 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