NYOJ0138-找球号(二)

找球号(二)

时间限制:1000 ms  |  内存限制:65535 KB
难度:5
描述
在某一国度里流行着一种游戏。游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=100000000),编号可重复,还有一个空箱子,现在有两种动作:一种是"ADD",表示向空箱子里放m(0<m<=100)个球,另一种是"QUERY”,表示说出M(0<M<=100)个随机整数ki(0<=ki<=100000100),分别判断编号为ki 的球是否在这个空箱子中(存在为"YES",否则为"NO"),先答出者为胜。现在有一个人想玩玩这个游戏,但他又很懒。他希望你能帮助他取得胜利。
输入
第一行有一个整数n(0<n<=10000);
随后有n行;
每行可能出现如下的任意一种形式:
第一种:
一个字符串"ADD",接着是一个整数m,随后有m个i;
第二种:
一个字符串"QUERY”,接着是一个整数M,随后有M个ki;

输出
输出每次询问的结果"YES"或"NO".
样例输入
2
ADD 5 34 343 54 6 2
QUERY 4 34 54 33 66
样例输出
YES
YES
NO
NO

哈希表模板题~~直接上代码↓↓↓↓↓

#include <stdio.h>
#include <malloc.h>
#define Mod 10000

//链式哈希:结点结构
struct Hash {
	int id;
	struct Hash *next;
} h[Mod+1];

char c[6];

//初始化哈希表
void init() {
	for( int i=1 ; i<=1000 ; i++ ) {
		h[i].id = i;
		h[i].next = NULL;
	}
}

int main() {
	bool flag;
	int t,n,v;
	struct Hash *temp;
	while( ~scanf( "%d",&t ) ) {
		init();
		while( t-- ) {
			scanf( "%s %d",c,&n );
//			printf( "11%s00%d00\n",c,n ) ;
			if( c[0]=='A' ) {
				//添加元素
				while( n-- ) {
					scanf( "%d",&v );
					//将h[i]后第一个结点设置为temp
					//即 h[i]->next = s; temp->next = h[i]->next ; h[i]->next = temp;
					//从而 h[i]->next = temp; temp->next = s;
					temp = ( struct Hash *)malloc( sizeof( struct Hash ) );
					temp->id = v;
					temp->next = h[v%Mod].next;
					h[v%Mod].next = temp;
				}
			} else {
				while( n-- ) {
					scanf( "%d",&v );
					temp = h[v%Mod].next;
					if( temp==NULL ) {
						printf( "NO\n" );
						continue;
					}
					while( temp ) {
						if( temp->id==v ) {
							printf( "YES\n" );
							break;
						}
						temp = temp->next;
					}
					if( temp==NULL )
						printf( "NO\n" );
				}
			}
		}
	}
}
啦啦啦~~~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值