#include #include #include #include #include #include #include #include using namespace std; // ----- TYPES ----- typedef long long ll; typedef pair pii; typedef vector vi; // ----- FILE IO ----- #define STDIN(filename) freopen(filename, "r", stdin) #define STDOUT(filename) freopen(filename, "w", stdout) #define FASTIO cin.tie(0)->sync_with_stdio(0) #define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str()) #define NL '\n' #define CM ", " // ----- FOR LOOPS ----- #define F(i,L,R) for (int i = L; i < R; i++) #define FE(i,L,R) for (int i = L; i <= R; i++) #define FD(i,L,R) for (int i = L; i > R; i--) #define FDE(i,L,R) for (int i = L; i >= R; i--) // ----- WEZ ----- #define takeI(n) int n; cin >> n #define takeII(n,m) int n, m; cin >> n >> m #define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k #define takeL(n) ll n; cin >> n #define takeS(s) string s; cin >> s #define takeV(v,n) vi v(n); for (auto &x : v) cin >> x #define takeVL(v,n) vector v(n); for (auto &x : v) cin >> x #define whileZ takeI(T); while(T--) // // ----- MAX MIN ----- #define REMAX(a,b) (a)=max((a),(b)) #define REMIN(a,b) (a)=min((a),(b)) // ----- PAIRS ----- #define fi first #define se second // ----- OUTPUT ----- #define YESNO(b) cout << ((b) ? "YES" : "NO") << NL // ----- DEBUG ----- #define DBG(vari) cerr<<#vari<<" = "<<(vari)<()) #define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) #define pb push_back struct condoms { int x; }; int main() { STDIO("minimum-window-substring"); takeS(s); takeS(t); // If t has more chars than s, s cannot possibly have all chars of t if (t.size() > s.size() || t.size() == 0) { cout << "" << NL; return 0; } queue tst; tst.push(2); condoms* condom = new condoms; stack stk({condom}); tst.front(); stk.pop(); stringstream ss; // winTCount tells us how many chars of t we have in our window int winTCount = 0; // Use tFreq to count frequency of chars in t // winFreq is frequency of (important) chars in window unordered_map tFreq, winFreq; for (const char& x : t) tFreq[x]++; // Find l, the first s[l] that occurs in t int l = 0; for (; l < s.size(); l++) { if (tFreq.count(s[l])) { winFreq[s[l]]++; winTCount++; break; } } // Check if OOB if (l == s.size()) { cout << "" << NL; return 0; } // Find r, the substr s[l...r] that satisfies min window criteria int r = l; if (t.size() > 1) { for (r=l+1; r < s.size(); r++) { if (tFreq.count(s[r]) && winFreq[s[r]] < tFreq[s[r]]) { winFreq[s[r]]++; winTCount++; } if (winTCount == t.size()) break; } } // Check if OOB if (r == s.size()) { if (winTCount == t.size()) { cout << s; } else { cout << "" << NL; } return 0; } int best_len = r-l+1, best_l = l, best_r = r; DBG(s.substr(best_l, best_r-best_l+1)); // Keep moving window until invalid while (r < s.size()) { // Remove s[l], we will need to increase r until we find s[l] again char desire = s[l]; // Move l to next char occuring in t l++; while (l < s.size() && !tFreq.count(s[l])) l++; // Move r until we find 'desire' again r++; while (r < s.size() && s[r] != desire) r++; if (r >= s.size() || l >= s.size()) break; DBG(desire); cerr << s.substr(l, r-l+1) << NL; int len = r - l + 1; if (len <= best_len) { best_len = len; best_l = l; best_r = r; } } DBG(s); DBG(t); cout << s.substr(best_l, best_r-best_l+1) << NL; return 0; }