hypothesist
BAN USER#include <vector>
#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std;
vector<pair<int, int> > func(vector<pair<int, int> > list) {
vector<pair<int, int> > ans;
sort(list.begin(), list.end());
ans.push_back(list[0]);
for(int i=1; i<list.size(); i++) {
pair<int, int> tmp=*(ans.end()-1);
if(list[i].first>=tmp.first && list[i].second<=tmp.second) // this means that list[i] is completely within tmp
continue;
else if(tmp.first>=list[i].first && tmp.second<=list[i].second)
ans[ans.size()-1]=list[i];
else {
if(list[i].first<tmp.first && list[i].first<tmp.second) {
ans[ans.size()-1].first=list[i].first;
ans[ans.size()-1].second=tmp.second;
}
else
ans.push_back(list[i]);
}
}
return ans;
}
int main() {
vector<pair<int, int> > in, out;
in.resize(4);
in[0].first=2; in[0].second=6;
in[1].first=3; in[1].second=5;
in[2].first=7; in[2].second=21;
in[3].first=20; in[3].second=21;
out=func(in);
for(int i=0; i<out.size(); i++)
printf("%d %d\n", out[i].first, out[i].second);
return 0;
}
str="ababcd"
- hypothesist October 20, 2015substr="abcd"
It fails because it sets j=0 at first mismatch, but doesn't check if the mismatch is the starting of the substring.