https://amazonindia.interviewstreet.com/challenges/dashboard/#problem/4fd648244715d 我对两个测试用例的回答是错误的,请告诉我它们是什么。我已经采用了一个包含 k 个元素的队列,如果新的元素形成一个小于前一个的新队列,它会删除前面的元素
vector<string> gud(vector<string> str,map<string,int> mymap,int tc,int cnt) {
int siz1=tc;
int count=0,count1=0,min=cnt,min_i=0;
int start=0,point=0;
bool first=true,neww=true;
list<string> window;
bool prev=false;
for(int i=1; i<str.size(); i++) {
string sr=str[i];
transform(sr.begin(), sr.end(), sr.begin(), ::tolower);
if(first) {
if( mymap.count(sr)>0) {
mymap[sr]-=1;
point=i;
start=sr.length();
window.push_back(sr);
first=false;
count++;
}
} else {
if(prev && count==tc) {
if(min>start) {
min=start;
min_i=point;
}
}
if(mymap.count(sr)>0) {
prev=true;
if(mymap[sr]>0) {
start+=sr.length();
window.push_back(sr);
count++;
mymap[sr]-=1;
}
else if((!window.empty()) && (sr.compare(window.front())==0)) {
point++;
window.pop_front();
window.push_back(sr);
while((!window.empty()) && (mymap.count(window.front())<1)) {
point++;
start-=window.front().length();
window.pop_front();
}
while((!window.empty()) && (mymap[window.front()]<0) ) {
point++;
start-=window.front().length();
mymap[window.front()]+=1;
window.pop_front();
while((!window.empty()) && (mymap.count(window.front())<1)) {
point++;
start-=window.front().length();
window.pop_front();
}
}
}//else if clse
else {
start+=sr.length();
window.push_back(sr);
mymap[sr]-=1;
prev=false;
}
}
else {
prev=false;
window.push_back(sr);
start+=sr.length();
// cout<<"inserting "<<sr<<endl;
}
}
}
if(prev && count==tc) {
if(min>start) {
min=start;
min_i=point;
}
}
if(count!=tc) {
cout<<"NO SUBSEGMENT FOUND";
vector<string> f;
f.push_back("");
return f;
}
int uo=0;
vector<string> give;
give.push_back("");
for(int i=min_i; i<str.size(); i++) {
if(uo>=min)break;
uo+=str[i].length();
give.push_back(str[i]);
}
return give;
}