【经典】凸包+hash+kmp——cf1017E
求两个凸包,问其中一个能否在平移,旋转之后与第二个重合
把凸包用 边-角-边 这样的形式hash出来,然后用字符串匹配的方式进行匹配即可
#include<bits/stdc++.h> using namespace std; #define N 200005 #define ll long long typedef double db; const db eps=1e-8; const db pi=acos(-1); int sign(db k){ if (k>eps) return 1; else if (k<-eps) return -1; return 0; } int cmp(db k1,db k2){return sign(k1-k2);} int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}// k3 在 [k1,k2] 内 struct point{ db x,y; point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};} point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};} point operator * (db k1) const{return (point){x*k1,y*k1};} point operator / (db k1) const{return (point){x/k1,y/k1};} int operator == (const point &k1) const{return cmp(x,k1.x)==0&&cmp(y,k1.y)==0;} bool operator < (const point k1) const{ int a=cmp(x,k1.x); if (a==-1) return 1; else if (a==1) return 0; else return cmp(y,k1.y)==-1; } db abs(){return sqrt(x*x+y*y);} db abs2(){return x*x+y*y;} db dis(point k1){return ((*this)-k1).abs();} point unit(){db w=abs(); return (point){x/w,y/w};} }; int inmid(point k1,point k2,point k3){return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y);} db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;} db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;} db rad(point k1,point k2){return atan2(cross(k1,k2),dot(k1,k2));} point getLL(point k1,point k2,point k3,point k4){ db w1=cross(k1-k3,k4-k3),w2=cross(k4-k3,k2-k3); return (k1*w2+k2*w1)/(w1+w2); } int intersect(db l1,db r1,db l2,db r2){ if (l1>r1) swap(l1,r1); if (l2>r2) swap(l2,r2); return cmp(r1,l2)!=-1&&cmp(r2,l1)!=-1; } int checkSS(point k1,point k2,point k3,point k4){ return intersect(k1.x,k2.x,k3.x,k4.x)&&intersect(k1.y,k2.y,k3.y,k4.y)&& sign(cross(k3-k1,k4-k1))*sign(cross(k3-k2,k4-k2))<=0&& sign(cross(k1-k3,k2-k3))*sign(cross(k1-k4,k2-k4))<=0; } vector<point> ConvexHull(vector<point>A,int flag=1){ int n=A.size(); vector<point>ans(n*2); sort(A.begin(),A.end()); int now=-1; for (int i=0;i<A.size();i++){ while (now>0&&sign(cross(ans[now]-ans[now-1],A[i]-ans[now-1]))<flag) now--; ans[++now]=A[i]; } int pre=now; for (int i=n-2;i>=0;i--){ while (now>pre&&sign(cross(ans[now]-ans[now-1],A[i]-ans[now-1]))<flag) now--; ans[++now]=A[i]; } ans.resize(now); return ans; } vector<point> v1,v2,con1,con2; int n,m; struct Node{ db len1,len2,cross; bool operator!=(const Node & a)const { if(sign(len1-a.len1)!=0 || sign(len2-a.len2)!=0 || sign(cross-a.cross)!=0)return 1; return 0; } }s1[N],s2[N]; int nxt[N]; void kmp_pre(){ int i,j; int len=con2.size(); j=nxt[0]=-1;i=0; while(i<len){ while(j!=-1 && s2[i]!=s2[j])j=nxt[j]; nxt[++i]=++j; } } void kmp(){ int i,j; int m=con2.size(); int n=con1.size()*2; i=j=0; while(i<n){ while(j!=-1 && s1[i]!=s2[j])j=nxt[j]; ++i,++j; if(j==m){ puts("YES");return; } } puts("NO"); } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ point k; scanf("%lf%lf",&k.x,&k.y); v1.push_back(k); } for(int i=1;i<=m;i++){ point k;scanf("%lf%lf",&k.x,&k.y); v2.push_back(k); } con1=ConvexHull(v1,1); con2=ConvexHull(v2,1); if(con1.size()!=con2.size()){puts("NO");return 0;} for(int i=0;i<con1.size();i++){ Node cur; point k1=con1[i],k2=con1[(i+1)%con1.size()],k3=con1[(i+2)%con1.size()]; cur.len1=k1.dis(k2); cur.len2=k2.dis(k3); cur.cross=dot(k1-k2,k3-k2); s1[i]=cur; } for(int i=0;i<con2.size();i++){ Node cur; point k1=con2[i],k2=con2[(i+1)%con1.size()],k3=con2[(i+2)%con1.size()]; cur.len1=k1.dis(k2); cur.len2=k2.dis(k3); cur.cross=dot(k1-k2,k3-k2); s2[i]=cur; } /*for(int i=0;i<con1.size();i++) cout<<con1[i].x<<" "<<con1[i].y<<'\n'; // cout<<s1[i].len1<<" "<<s1[i].len2<<" "<<s1[i].cross<<"\n"; for(int i=0;i<con1.size();i++) cout<<con2[i].x<<" "<<con2[i].y<<'\n'; // cout<<s2[i].len1<<" "<<s2[i].len2<<" "<<s2[i].cross<<"\n"; */ kmp_pre(); for(int i=0;i<con1.size();i++) s1[i+con1.size()]=s1[i]; kmp(); }