您的位置:首页 > 其它

2017 计蒜之道 初赛 第一场 A题B题

2017-05-20 21:12 190 查看
 A. 阿里的新游戏 

通过率: 81.04 %通过人数: 1090分值: 500

 B. 阿里天池的新任务(简单)

通过率: 61.89 %通过人数: 588分值: 1000

 C. 阿里天池的新任务(中等)

通过率: 3.24 %通过人数: 9分值: 1500

 D. 阿里天池的新任务(困难)

通过率: 4.6 %通过人数: 4

A:直接暴力所有情况

#include<stdio.h>
#include<string.h>
int a[200][200];
int d=100;
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[d+x][d+y]=1;
}
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
}
int ans=0;
//内圈
int t=1;
if(a[d+t][d-t]==1&&a[d+t][d]==1&&a[d+t][d+t]==1) //有
ans++;
if(a[d-t][d-t]==1&&a[d-t][d]==1&&a[d-t][d+t]==1) //左
ans++;
if(a[d+t][d+t]==1&&a[d][d+t]==1&&a[d-t][d+t]==1) //shang
ans++;
if(a[d+t][d-t]==1&&a[d][d-t]==1&&a[d-t][d-t]==1)
ans++;

//中
t=2;
if(a[d+t][d-t]==1&&a[d+t][d]==1&&a[d+t][d+t]==1)
ans++;
if(a[d-t][d-t]==1&&a[d-t][d]==1&&a[d-t][d+t]==1) //左
ans++;
if(a[d+t][d+t]==1&&a[d][d+t]==1&&a[d-t][d+t]==1) //shang
ans++;
if(a[d+t][d-t]==1&&a[d][d-t]==1&&a[d-t][d-t]==1)
ans++;

//外圈
t=3;
if(a[d+t][d-t]==1&&a[d+t][d]==1&&a[d+t][d+t]==1)
ans++;
if(a[d-t][d-t]==1&&a[d-t][d]==1&&a[d-t][d+t]==1) //左
ans++;
if(a[d+t][d+t]==1&&a[d][d+t]==1&&a[d-t][d+t]==1) //shang
ans++;
if(a[d+t][d-t]==1&&a[d][d-t]==1&&a[d-t][d-t]==1)
ans++;

//轴
if(a[d+1][d]==1&&a[d+2][d]==1&&a[d+3][d]==1)
ans++;
if(a[d-1][d]==1&&a[d-2][d]==1&&a[d-3][d]==1) //左
ans++;
if(a[d][d+1]==1&&a[d][d+2]==1&&a[d][d+3]==1) //shang
ans++;
if(a[d][d-1]==1&&a[d][d-2]==1&&a[d][d-3]==1)
ans++;
printf("%d\n",ans);
}
return 0;
}



B:kmp算法字符串匹配
#include<stdio.h>
#include<string.h>
typedef long long ll;
ll n,a,b,L,R,ans;
char ts[1010101];
int f[1001000];
char make(int wi)
{
if(L<=wi&&wi<=R)
{
if(wi%2==0)
return 'A';
else
return 'T';
}
else
{
if(wi%2==0)
return 'G';
else
return 'C';
}
}
void getfail(char p[],int f[]) //字符串p自我匹配
{
int len
c807
=strlen(p);
f[0]=f[1]=0;
for(int i=1;i<len;i++)
{
int j=f[i];
while(j&&p[i]!=p[j])
j=f[j];
if(p[i]==p[j])
f[i+1]=j+1;//多匹配到了一个字符
else
f[i+1]=0;//该字符配不上
}
}
int find(char*P, int*f)//p去匹配字符串T
{
int m = strlen(P);
getfail(P, f); //得出部分匹配表
int j = 0; //短串的下标
for(int i = 0; i < n; i++) //长串下标
{
while(j && P[j] != make((b+i*a)%n))//突然失配了
{
j = f[j]; //j往回退,直到0或者上一个字符相等的位置
}
if(P[j] == make((b+i*a)%n))
{
j++; //匹配了一个字符,j++
}
if(j == m) //短串匹配到头了
{
ans++;
//return i - m + 1;//返回成功匹配的起点字符位置
}
}
return -1;
}

int main()
{

while(~scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&L,&R))
{
scanf("%s",ts);
ans=0;
find( ts, f);

printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: