标签:
假设S[n]表示n条线段中最长重叠距离,最长重叠距离只与两条线段有关,那么考虑两种情况:
1. 最长重叠距离与第n条线段无关,则最长重叠距离存在于前n-1条线段中,即S[n]=S[n-1];
2. 最长重叠距离与第n条线段有关,则最长重叠距离为第n条线段与前面n-1条线段中的最大重叠距离者,S[n]=max(overlap(segment[n],segment[i])), i=1....n-1
因此得到状态转移方程:
S[n]=0; n<=1
S[2]=overlap(segment[0],segment[1])
S[n]=max(S[n-1],max(overlap(segment[n],segment[i])))
#include <iostream> #include <algorithm> using namespace std; typedef struct{ int start; int end; }segment; bool isShorter(const segment &s1,const segment &s2); segment commonSegment(const segment &seg_a,const segment &seg_b); int findLongestSegment(const vector<segment> &segments,int size,segment &maxSegment); int main() { int n; int start,end; while(cin>>n){ vector<segment> segments(n); for(int i=0;i<n;i++){ if(cin>>start && cin>>end){ segments[i].start=start; segments[i].end=end; } } // sort by segment.end sort(segments.begin(),segments.end(),isShorter); segment maxSeg; int maxLen; maxLen=findLongestSegment(segments,n,maxSeg); cout<<"Longest Length:"<<maxLen<<endl; cout<<"start:"<<maxSeg.start<<endl; cout<<"end:"<<maxSeg.end<<endl; } return 0; } bool isShorter(const segment &s1,const segment &s2){ return s1.end<s2.end; } //assume seg_a.end<seg_b.end segment commonSegment(const segment &seg_a,const segment &seg_b){ segment commonSeg; if(seg_a.end<seg_b.start){ commonSeg.start=0; commonSeg.end=0; } else{ commonSeg.start=seg_b.start; commonSeg.end=seg_a.end; } return commonSeg; } int findLongestSegment(const vector<segment> &segments,int size,segment &maxSegment){ if(size<=0) return 0; /* if(size==1){ maxSegment.start=segments[0].start; maxSegment.end=segments[0].end; return maxSegment.end-maxSegment.start; } */ if(size==2){ maxSegment=commonSegment(segments[0],segments[1]); return maxSegment.end-maxSegment.start; } // size>2 // dynamic programming int maxLen,tmpLen; segment tmpMaxSeg; maxLen=findLongestSegment(segments,size-1,tmpMaxSeg); maxSegment=tmpMaxSeg; // maxSegment=(maxSegment,commonSeg) segment commonSeg; for(int i=0;i<size-1;i++){ commonSeg=commonSegment(segments[i],segments[size-1]); tmpLen=commonSeg.end-commonSeg.start; if(tmpLen>maxLen){ maxLen=tmpLen; maxSegment=commonSeg; } } return maxSegment.end-maxSegment.start; }
标签:
原文地址:http://www.cnblogs.com/AndyJee/p/4537376.html