結果

問題 No.1192 半部分列
ユーザー MisterMister
提出日時 2020-08-22 15:29:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,596 bytes
コンパイル時間 870 ms
コンパイル使用メモリ 81,236 KB
実行使用メモリ 30,592 KB
最終ジャッジ日時 2024-04-23 09:47:54
合計ジャッジ時間 2,194 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 AC 40 ms
30,592 KB
testcase_07 AC 36 ms
30,464 KB
testcase_08 AC 37 ms
30,592 KB
testcase_09 AC 34 ms
29,184 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 36 ms
30,336 KB
testcase_13 AC 17 ms
15,232 KB
testcase_14 AC 24 ms
20,480 KB
testcase_15 AC 23 ms
19,456 KB
testcase_16 AC 17 ms
15,104 KB
testcase_17 AC 23 ms
19,840 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
16,640 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 37 ms
30,464 KB
testcase_24 AC 7 ms
7,808 KB
testcase_25 AC 15 ms
14,336 KB
testcase_26 AC 18 ms
14,592 KB
testcase_27 AC 18 ms
16,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

void solve() {
    std::string s, t;
    std::cin >> s >> t;

    int n = s.length(), m = t.length();
    s.insert(s.begin(), '$');
    t.insert(t.begin(), '$');

    std::vector<std::vector<int>> snext(n + 1);
    {
        std::vector<int> next(26, n + 1);
        for (int i = n; i >= 0; --i) {
            snext[i] = next;
            if (i > 0) next[s[i] - 'a'] = i;
        }
    }

    std::vector<std::vector<int>> tnext(m + 1);
    {
        std::vector<int> next(26, m + 1);
        for (int i = m; i >= 0; --i) {
            tnext[i] = next;
            if (i > 0) next[t[i] - 'a'] = i;
        }
    }

    std::vector<int> include(n + 2, -1);
    include[n + 1] = m + 1;
    {
        int j = m;
        for (int i = n; i >= 0; --i) {
            while (j >= 0 && t[j] != s[i]) --j;

            include[i] = j--;
            if (j < 0) break;
        }
    }

    if (include[0] != -1) {
        std::cout << "-1\n";
        return;
    }

    std::string ans;
    int i = 0, j = 0;
    while (i <= n && j <= m) {
        for (int c = 0; c < 26; ++c) {
            int ni = snext[i][c],
                nj = tnext[j][c];
            if (ni > n || nj <= include[ni]) continue;

            ans.push_back(c + 'a');
            i = ni;
            j = nj;
            break;
        }
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0