#include #include #include #include using namespace std; const int inf = 1012345678; vector > calc_next(string S) { int N = S.size(); vector > dp(N + 1, vector(26, inf)); for (int i = N - 1; i >= 0; --i) { dp[i] = dp[i + 1]; dp[i][S[i] - 'a'] = i; } return dp; } int main() { string S, T; cin >> S >> T; int LS = S.size(), LT = T.size(); vector dp(LS + 1); dp[LS] = LT; int ptr = LT; for (int i = LS - 1; i >= 0; --i) { while (ptr != -1) { --ptr; if (ptr != -1 && S[i] == T[ptr]) break; } dp[i] = ptr; } vector > ns = calc_next(S); vector > nt = calc_next(T); string ans; int posa = 0, posb = 0; while (true) { bool found = false; for (int i = 0; i < 26; ++i) { if (ns[posa][i] != inf && dp[ns[posa][i] + 1] <= nt[posb][i]) { posa = ns[posa][i] + 1; posb = nt[posb][i] + 1; ans += char('a' + i); found = true; break; } } if (posb >= inf) { break; } if (!found) break; } if (ans.empty()) cout << -1 << endl; else cout << ans << endl; return 0; }