#include #include #include #include using namespace std; const int alphabet_size = 26; string s, t; void solve() { int ss = (int)s.size(), ts = (int)t.size(); vector> t_next(ts + 1, vector(alphabet_size, ts)); for (int i = ts - 1; i >= 0; --i) { for (int j = 0; j < alphabet_size; ++j) { t_next[i][j] = t_next[i + 1][j]; } t_next[i][t[i] - 'a'] = i; } string ans = "zzzzzzzzzzzzzzzzzzzzzz"; for (int bit = 0; bit < 1 << ss; ++bit) { if (!bit) continue; string str; for (int i = 0; i < ss; ++i) { if ((bit >> i) & 1) str.push_back(s[i]); } int now = 0; for (char c: str) { now = t_next[now][c - 'a'] + 1; if (now == ts + 1) break; } if (now == ts + 1) ans = min(ans, str); } if (ans == "zzzzzzzzzzzzzzzzzzzzzz") cout << -1 << endl; else cout << ans << endl; } int main() { cin >> s >> t; // small case assert(s.size() < 20 && t.size() < 20); solve(); return 0; }