区间合并
#include <iostream>
#include <map>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
struct Interval{
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
int ReturnNum(string s,int i,int j,int len){
int num = 0;
for(int a=i+1;a<j;a++){
num+=(s[a]-'0')*pow(10,--len);
}
return num;
}
void CutString(string s,map<int,int> &ans){
int tmp1=0,tmp2=0,tmp3=0;
int num1 = 0,num2 = 0;
for(int i=0;i<s.length();i++){
if(s[i] == '[')
tmp1 = i;
if(s[i] == '-')
tmp2 = i;
if(s[i] == ')'){
tmp3 = i;
num1 = ReturnNum(s,tmp1,tmp2,tmp2-tmp1-1);
num2 = ReturnNum(s,tmp2,tmp3,tmp3-tmp2-1);
if(ans[num1] != NULL && ans[num1] < num2)
ans[num1] = num2;
if(ans[num1] == NULL)
ans[num1] = num2;
}
}
}
void Print(int a,int b){
cout << "[";
cout << a;
cout << "-";
cout << b;
cout << ")";
}
int main(){
string s1,s2;
map<int,int> input;
vector<Interval>intervals;
vector<Interval> result;
while(cin >> s1){
input.clear();
intervals.clear();
result.clear();
CutString(s1,input);
cin >> s2;
CutString(s2,input);
map<int,int>::iterator it;
int q=0;
int s,e;
for(it=input.begin();it!=input.end();it++){
s = it->first;
e = it->second;
intervals.push_back(Interval(s,e));
q++;
}
int left = intervals[0].start;
int right = intervals[0].end;
for(int i=1;i<intervals.size();i++){
if(intervals[i].start<=right){
right = max(intervals[i].end,right);
}
else{
result.push_back(Interval(left,right));
left = intervals[i].start;
right = intervals[i].end;
}
}
result.push_back(Interval(left,right));
for(int i=0;i<result.size()-1;i++){
Print(result[i].start,result[i].end);
cout <<" ,";
}
Print(result[result.size()-1].start,result[result.size()-1].end);
cout << endl;
}
return 0;
}
/*
[5-60),[75-100),[120-160)
[70-90)
*/