滑动窗口
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
int start = 0;
int len = INT_MAX;//返回结果
void getRes(string& S,string& T) {
int left = 0, right = 0;
int valid = 0;//valid等于字符串的字符数了,就是匹配上了
unordered_map<char,int> need, windows;
for (char i : T) need[i]++;
while (right < S.size()) {//right等于S.size()时,结束,即超过字符串索引
//窗口扩大
char c = S[right];
right++;
if (need.count(c)) {
windows[c]++;
if (need[c] == windows[c])
valid++;
}
//窗口缩小
while (valid == need.size()) {
if (right - left < len) {
start = left;
len = right - left;
}
char c = S[left];
left++;
if (need.count(c)) {
if (windows[c] == need[c])
valid--;
windows[c]--;
}
}
}
}
int main() {
string s = "adafsasdfasfdsdsa";
string t="adsf";
getRes(s,t);
cout << string(s.begin() + start, s.begin() + start + len);
}