如何给题解审核员枯燥无味的生活添上一抹五彩斑斓的黑(2)
Register_int · · 个人记录
本蒟蒻的第二篇题解,好激动啊(我像个彩笔)
这题挺简单的。。
首先要读题呀~~~
来吧,让我们康康题目~
我们来看哈,要求异或部分,并且要顺时针输出~~
输入有若干组,每两行为一组,表示输入的两个多边形。每一行表示一个多边形,每行包含2n+1个数,第一个数是n,表示多边形顶点的个数,然后n对数表示多边形每个顶点的坐标(xi,yi),按照顺时针顺序给出。输入以一行单独的一个0结束。
直接上AC代码!
(请君勿抄,小心去世哦)
include <cmath>
include <cstdio>
include <cstdlib>
include <iostream>
include <cstring>
include <string>
include <algorithm>
include <queue>
include <stack>
include <climits>
using namespace std;
const int MAX = 1100;
struct point {double x,y;};
struct line { point a,b; double ang;};
point p[MAX],s[MAX];
line ln[MAX2],deq[MAX2];
const double eps = 1e-6;
bool dy(double x,double y) { return x > y + eps;} // x > y
bool xy(double x,double y) { return x < y - eps;} // x < y
bool dyd(double x,double y) { return x > y - eps;} // x >= y
bool xyd(double x,double y) { return x < y + eps;} // x <= y
bool dd(double x,double y) { return fabs( x - y ) < eps;} // x == y
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向
{
return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool parallel(line u,line v)
{
return dd( (u.a.x - u.b.x)*(v.a.y - v.b.y) - (v.a.x - v.b.x)*(u.a.y - u.b.y) , 0.0 );
}
point l2l_inst_p(line l1,line l2)
{
point ans = l1.a;
double t = ((l1.a.x - l2.a.x)*(l2.a.y - l2.b.y) - (l1.a.y - l2.a.y)*(l2.a.x - l2.b.x))/
((l1.a.x - l1.b.x)*(l2.a.y - l2.b.y) - (l1.a.y - l1.b.y)*(l2.a.x - l2.b.x));
ans.x += (l1.b.x - l1.a.x)*t;
ans.y += (l1.b.y - l1.a.y)*t;
return ans;
}
bool equal_ang(line a,line b) // 第一次unique的比较函数
{
return dd(a.ang,b.ang);
}
bool cmphp(line a,line b) // 排序的比较函数
{
if( dd(a.ang,b.ang) ) return xy(crossProduct(b.a,b.b,a.a),0.0);
return xy(a.ang,b.ang);
}
bool equal_p(point a,point b)//第二次unique的比较函数
{
return dd(a.x,b.x) && dd(a.y,b.y);
}
void makeline_hp(point a,point b,line &l) // 线段(向量ab)左侧区域有效
{
l.a = a; l.b = b;
l.ang = atan2(a.y - b.y,a.x - b.x); // 如果是右侧区域,改成b.y - a.y,b.x - a.x
}
void inst_hp_nlogn(line ln,int n,point s,int &len)
{
len = 0;
sort(ln,ln+n,cmphp);
n = unique(ln,ln+n,equal_ang) - ln;
int bot = 0,top = 1;
deq[0] = ln[0]; deq[1] = ln[1];
for(int i=2; i<n; i++)
{
if( parallel(deq[top],deq[top-1]) || parallel(deq[bot],deq[bot+1]) )
return ;
while( bot < top && dy(crossProduct(ln[i].a,ln[i].b,
l2l_inst_p(deq[top],deq[top-1])),0.0) )
top--;
while( bot < top && dy(crossProduct(ln[i].a,ln[i].b,
l2l_inst_p(deq[bot],deq[bot+1])),0.0) )
bot++;
deq[++top] = ln[i];
}
while( bot < top && dy(crossProduct(deq[bot].a,deq[bot].b,
l2l_inst_p(deq[top],deq[top-1])),0.0) ) top--;
while( bot < top && dy(crossProduct(deq[top].a,deq[top].b,
l2l_inst_p(deq[bot],deq[bot+1])),0.0) ) bot++;
if( top <= bot + 1 ) return ;
for(int i=bot; i<top; i++)
s[len++] = l2l_inst_p(deq[i],deq[i+1]);
if( bot < top + 1 ) s[len++] = l2l_inst_p(deq[bot],deq[top]);
len = unique(s,s+len,equal_p) - s;
}
double area_polygon(point p[],int n)
{
if( n < 3 ) return 0.0;
double s = 0.0;
for(int i=0; i<n; i++)
s += p[(i+1)%n].y * p[i].x - p[(i+1)%n].x * p[i].y;
return fabs(s/2.0);
}
int main()
{
int n,len,m;
while ( ~scanf("%d",&n) && n )
{
for(int i=0; i<n; i++)
scanf("%lf %lf",&p[i].x,&p[i].y);
p[n] = p[0];
double a1 = area_polygon(p,n);
for(int i=0; i<n; i++)
makeline_hp(p[i+1],p[i],ln[i]);
scanf("%d",&m);
for(int i=0; i<m; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
double a2 = area_polygon(p,m);
p[m] = p[0];
for(int i=0; i<m; i++)
makeline_hp(p[i+1],p[i],ln[i+n]);
n += m;
inst_hp_nlogn(ln,n,s,len);
double area = a1 + a2 - 2*area_polygon(s,len);
printf("%8.2lf",area);
}
printf("\n");
return 0;
}
1 By CSDNdalao
include<bits/stdc++.h>
using namespace std; int n,h[10005]; string s,t; int gethash(string t){ int sum=0,len=t.length(); for(int i=0;i<len;i++){ sum+=t[i]*(t[i]-'a'+2); } return sum; } int main(){ cin>>s>>n; for(int i=0;i<n;i++){ cin>>t; h[i]=gethash(t); } int len=s.length(),cnt=0; for(int i=0;i<len-7;i++){ string tmp=s.substr(i,8); int ha=gethash(tmp); for(int j=0;j<n;j++){ if(ha==h[j])cnt++; } } cout<<cnt; return 0; }