514A. Chewbaсca and Number
大于4的就替换
开头特判
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;++i)
#define drep(i,t,s) for(int i=t;i>=s;--i)
#define inf 0x3f3f3f3f
using namespace std;
char st[100];
int main(){
scanf("%s",st);
int len=strlen(st);
rep(i,0,len-1){
int x=st[i]-'0';
if (x==9 && i==0) continue;
if (x>=5) x=9-x,st[i]=x+'0';
}
printf("%s",st);
return 0;
}
514B.Han Solo and Lazer Gun
每次任选一个点和Gun构成直线,查找这个直线上还没有被消灭的目标,标记为消灭,用叉积=0判断共线
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;++i)
#define drep(i,t,s) for(int i=t;i>=s;--i)
#define inf 0x3f3f3f3f
using namespace std;
struct Point{
long long x,y;
}p[1005];
bool vis[1005];
long long cross(const Point &a,const Point &b,const Point &c){
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int n,ans=0;
int main(){
scanf("%d%I64d%I64d",&n,&p[0].x,&p[0].y);
rep(i,1,n)
scanf("%I64d%I64d",&p[i].x,&p[i].y);
rep(i,1,n)
if (!vis[i]){
vis[i]=1;
++ans;
rep(j,i+1,n)
if ((!vis[j]) && (cross(p[0],p[i],p[j])==0)) vis[j]=1;
}
printf("%d\n",ans);
return 0;
}
514C.Watto and Mechanism
官方题解是hash+二分查找
我的方法是建一颗Trie树,询问就在树上dfs
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;++i)
#define drep(i,t,s) for(int i=t;i>=s;--i)
#define inf 0x3f3f3f3f
using namespace std;
bool f;
int n,m,son[600600][3],idx;
char st[600600];
bool search(int now,int x,bool flag){
if (st[x]== 0) f|=flag;
if (!f && st[x]>0){
int k=st[x]-'a';
//a,b,c
rep(i,0,2)
if (son[now][i]){
if (k==i) search(son[now][i],x+1,flag);
else if (!flag) search(son[now][i],x+1,true);
}
}
}
int main(){
scanf("%d%d",&n,&m);
rep(i,1,n){
scanf("%s",st);
int u=0,len=strlen(st);
rep(j,0,len-1){
int k=st[j]-'a';
if (!son[u][k]) son[u][k]=++idx;
u=son[u][k];
}
}
rep(i,1,m){
scanf("%s",st);
f=false;
search(0,0,0);
if (f) puts("YES"); else puts("NO");
}
return 0;
}