結果

問題 No.1192 半部分列
ユーザー MayimgMayimg
提出日時 2020-08-25 18:29:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,456 bytes
コンパイル時間 1,899 ms
コンパイル使用メモリ 200,376 KB
実行使用メモリ 30,592 KB
最終ジャッジ日時 2024-04-24 04:44:40
合計ジャッジ時間 2,988 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 40 ms
30,592 KB
testcase_07 AC 37 ms
30,208 KB
testcase_08 AC 38 ms
30,464 KB
testcase_09 AC 35 ms
29,312 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 38 ms
30,592 KB
testcase_13 AC 16 ms
15,104 KB
testcase_14 AC 23 ms
20,480 KB
testcase_15 AC 21 ms
19,328 KB
testcase_16 AC 15 ms
15,232 KB
testcase_17 AC 22 ms
19,840 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 8 ms
7,936 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 40 ms
30,336 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  string s, t;
  cin >> s >> t;
  int n = s.size(), m = t.size();
  vector<int> ls(n + 1, -1);
  for (int i = n - 1, j = m - 1; i >= 0; i--) {
    while (j >= 0 && t[j] != s[i]) j--;
    ls[i] = j;
    if (j >= 0) j--;
  }
  if (ls[0] != -1) {
    cout << -1 << endl;
    return 0;
  }
  //cerr << "here" << endl;
  vector<vector<int>> as(n + 1, vector<int>(26, -1));
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++) {
      if (j == s[i] - 'a') {
        as[i][j] = i;
      } else {
        if (i < n - 1) as[i][j] = as[i + 1][j];
      }
    }
  }
  vector<vector<int>> at(m + 1, vector<int>(26, -1));
  for (int i = m - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++) {
      if (j == t[i] - 'a') {
        at[i][j] = i;
      } else {
        if (i < m - 1) at[i][j] = at[i + 1][j];
      }
    }
  }
  string ans;
  int i = 0, j = 0;
  while (i < n) {
    bool found = false;
    for (int c = 0; c < 26; c++) {
      if (as[i][c] == -1) continue;
      if (at[j][c] == -1) {
        ans += c + 'a';
        goto done; 
      }
      int a = as[i][c];
      int b = at[j][c];
      if (ls[a] == -1 || ls[a] < b) {
        ans += c + 'a';
        i = a + 1;
        j = b + 1;
        found = true;
        break;
      }
    }
    assert(found);
  }
  done:;
  cout << ans << endl;
  return 0;
}
0