#include #include #include #include #include #include #include #include #include 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; }