結果

問題 No.1192 半部分列
ユーザー Mister
提出日時 2020-08-22 15:29:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 1,596 bytes
コンパイル時間 2,571 ms
コンパイル使用メモリ 78,096 KB
最終ジャッジ日時 2025-01-13 09:36:09
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

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