結果

問題 No.1192 半部分列
ユーザー ForestedForested
提出日時 2020-07-30 10:03:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,846 bytes
コンパイル時間 742 ms
コンパイル使用メモリ 74,284 KB
実行使用メモリ 30,448 KB
最終ジャッジ日時 2023-09-20 23:50:50
合計ジャッジ時間 2,829 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 45 ms
30,448 KB
testcase_07 AC 43 ms
30,212 KB
testcase_08 AC 43 ms
30,276 KB
testcase_09 AC 41 ms
29,100 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 43 ms
30,276 KB
testcase_13 AC 19 ms
14,924 KB
testcase_14 AC 28 ms
20,304 KB
testcase_15 AC 27 ms
19,168 KB
testcase_16 AC 20 ms
14,916 KB
testcase_17 AC 27 ms
19,672 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 8 ms
7,840 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 22 ms
16,500 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 43 ms
30,236 KB
testcase_24 AC 8 ms
7,796 KB
testcase_25 AC 19 ms
14,520 KB
testcase_26 AC 19 ms
14,524 KB
testcase_27 AC 22 ms
16,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int alphabet_size = 26;

int main() {
    string s, t;
    cin >> s >> t;
    
    int ss = s.size(), ts = t.size();
    vector<vector<int>> s_next(ss + 1, vector<int>(alphabet_size, ss));
    for (int i = ss - 1; i >= 0; --i) {
        for (int j = 0; j < alphabet_size; ++j) {
            s_next[i][j] = s_next[i + 1][j];
        }
        s_next[i][s[i] - 'a'] = i;
    }
    vector<vector<int>> t_next(ts + 1, vector<int>(alphabet_size, ts));
    for (int i = ts - 1; i >= 0; --i) {
        for (int j = 0; j < alphabet_size; ++j) {
            t_next[i][j] = t_next[i + 1][j];
        }
        t_next[i][t[i] - 'a'] = i;
    }
    
    vector<int> back_substr(ss + 1, ts);
    int k = ts;
    for (int i = ss - 1; i >= 0; --i) {
        while (k > 0) {
            --k;
            if (s[i] == t[k]) {
                back_substr[i] = k;
                break;
            }
        }
        if (back_substr[i] == ts) back_substr[i] = -1;
    }
    
    if (back_substr[0] != -1) {
        cout << -1 << endl;
        return 0;
    }
    
    string ans;
    int s_cnt = 0, t_cnt = 0;
    bool ended = false;
    while (!ended) {
        for (int i = 0; i < alphabet_size; ++i) {
            if (s_next[s_cnt][i] == ss) {
                continue;
            } else if (t_next[t_cnt][i] == ts) {
                ans.push_back('a' + i);
                ended = true;
                break;
            } else {
                if (back_substr[s_next[s_cnt][i] + 1] < t_next[t_cnt][i] + 1) {
                    s_cnt = s_next[s_cnt][i] + 1;
                    t_cnt = t_next[t_cnt][i] + 1;
                    ans.push_back('a' + i);
                    break;
                }
            }
        }
    }
    
    cout << ans << endl;
    return 0;
}
0