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

 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=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;
}






  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值