南阳理工OJ 86 找球号(一)外挂 哈希 字典树

 

 

/*
字典树
*/

#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct node
{
	bool f;
	struct node*p[10];
}root;
void init(node *p)
{
	p->f=false;
	for(int i=0;i<10;i++)
		p->p[i]=NULL;
}
node * xin()
{
	node *p =(node*)malloc(sizeof(node));
	init(p);
	return(p);
}
void add(char *str)
{
	int n=strlen(str);
	node * p=&root;
	for(int i=0;i<n;i++)
	{
		if(p->p[str[i]-'0']==NULL)
			p->p[str[i]-'0']=xin();	
		p=p->p[str[i]-'0'];
	}
	p->f=true;
}
bool find(char *str)
{
	int n=strlen(str);
	node * p=&root;
	for(int i=0;i<n;i++)
	{
		if(p->p[str[i]-'0']==NULL)return false;
		p=p->p[str[i]-'0'];
	}
	return p->f;
}
int main()
{
	int m,n;
	char term[15];
	scanf("%d%d",&m,&n);
	init(&root);
	while(m--)
	{
		scanf("%s",term);
		add(term);
	}
	while(n--)
	{
		scanf("%s",term);
		if(find(term))printf("YES\n");
		else printf("NO\n");
	}
	return(0);
}

 

 

 

 

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int hash[3125100],n,m;
int main()
{
   // freopen("in.txt","r",stdin);
    scanf("%d %d",&n,&m);
//    memset(hash,0,sizeof(hash));
    int x,t,mod;
    char ch;
    for(int i=0; i<n; i++)
    {
        // scanf("%d",&x);
        x=0;
        while(!((ch=getchar())>='0'&&ch<='9'))
        {
        }
        x=(ch-'0');
        while((ch=getchar())>='0' && ch<='9')
            x=x*10+(ch-'0');
        t=x>>5;
        mod=x-(t<<5);
        hash[t]|=(1<<mod);
    }
    for(int i=0; i<m; i++)
    {
//            scanf("%d",&x);
        x=0;
        while(!((ch=getchar())>='0'&&ch<='9'))
        {
        }
        x=(ch-'0');
        while((ch=getchar())>='0' && ch<='9')
            x=x*10+(ch-'0');
        t=x>>5;
        mod=x-(t<<5);
        if(hash[t]&(1<<mod))
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 

 

 

 

第二种方法用到了位运算,开不了那么多的int或者bool ,但可以开那么多的位,照样能存储一样的信息,

还有输入输出用到了getchar()用这个接收字符再算成整数 都比scanf 快的多!!!不信试试!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值