#include #ifndef DUMP #define DUMP(...) (void)0 #endif using namespace std; int main() { cin.tie(nullptr)->sync_with_stdio(false); string s, t; cin >> s >> t; int n = size(s), m = size(t); vector p(n + 1); p[n] = m; for (int i = n; i--;) { p[i] = p[i + 1] - 1; while (p[i] >= 0 and t[p[i]] != s[i]) --p[i]; } if (p[0] >= 0) { cout << "-1\n"; exit(0); } vector> nxt(n + 1); nxt[n].fill(n); for (int i = n; i--;) { nxt[i] = nxt[i + 1]; nxt[i][s[i] - 'a'] = i; } string res; int i = 0, j = 0; while (true) { DUMP(i, j); for (char c = 'a'; c <= 'z'; ++c) { if (p[nxt[i][c - 'a']] >= j) continue; res += c; i = nxt[i][c - 'a'] + 1; while (j < m and t[j] != c) ++j; if (j == m) { cout << res << '\n'; exit(0); } ++j; break; } } cout << res << '\n'; }