結果
問題 |
No.1192 半部分列
|
ユーザー |
![]() |
提出日時 | 2021-03-05 18:59:48 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 17 ms / 2,000 ms |
コード長 | 1,462 bytes |
コンパイル時間 | 869 ms |
コンパイル使用メモリ | 85,712 KB |
実行使用メモリ | 24,476 KB |
最終ジャッジ日時 | 2024-10-06 20:58:49 |
合計ジャッジ時間 | 2,008 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 25 |
ソースコード
#include <iostream> #include <map> #include <set> #include <queue> #include <cmath> #include <vector> #include <string> #include <algorithm> #include <functional> using namespace std; #pragma warning (disable: 4996) string S, T; int cs[100009][26]; int ct[100009][26]; int dp[100009]; int main() { // Step #1. 入力 cin >> S >> T; // Step #2. S について計算 for (int i = 0; i < 26; i++) cs[S.size()][i] = S.size() + 2; for (int i = S.size() - 1; i >= 0; i--) { for (int j = 0; j < 26; j++) cs[i][j] = cs[i + 1][j]; cs[i][S[i] - 'a'] = i; } // Step #3. T について計算 for (int i = 0; i < 26; i++) ct[T.size()][i] = T.size() + 2; for (int i = T.size() - 1; i >= 0; i--) { for (int j = 0; j < 26; j++) ct[i][j] = ct[i + 1][j]; ct[i][T[i] - 'a'] = i; } // Step #4. 貪欲法 dp[S.size()] = T.size(); int cx = T.size() - 1; for (int i = S.size() - 1; i >= 0; i--) { while (cx >= 0 && T[cx] != S[i]) cx--; dp[i] = cx; cx--; } // Step #5. 辞書順貪欲 int px = 0, qx = 0; string str = ""; while (qx <= T.size()) { bool flag = false; for (int i = 0; i < 26; i++) { int nex1 = cs[px][i]; int nex2 = ct[qx][i]; if (nex1 == S.size() + 2) continue; if (dp[nex1 + 1] < nex2 + 1) { flag = true; str += ('a' + i); px = nex1 + 1; qx = nex2 + 1; break; } } if (flag == false) break; } // Step #6. 出力 if (str == "") cout << "-1" << endl; else cout << str << endl; return 0; }