题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2141

Can you find it?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 18192    Accepted Submission(s): 4601

Problem Description
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.
 
Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.
 
Output
For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".
 
Sample Input
3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10
 
Sample Output
Case 1:
NO
YES
NO
 
Author
wangye
 
Source

题意: 从三个集合中分别各取一个数,然后相加等于X

注意数据范围很大所以不可能暴力,先算出前两个集合中任意两个值得和保存到数组d中,然后对d进行去重操作,注意:去重函数unique(d,d+cc)返回的是最后一个元素所在的地址,要获得新数组的总元素个数要减去首地址及  int n = unique(d,d+cc) - d ;

这里要注意,两个数相加可能会超int,所以要用long long,而且在每次输入x的时候在对应的d数组中找是否存在x-c的时候必须用O(n)的算法,所以考虑到用哈希的方法,最慢的哈希也要比二分快。

 #include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
ll a[],b[],c[],d[]; const int Mod = ;
int inf = ;
ll mp[Mod];
void insert(ll u)
{
ll v = u;
if(v<) v*=-;
int id = v%Mod;
while(mp[id]!=inf) {id++;id%=Mod;}
mp[id] = u;
}
bool find(ll u){
ll v = u;
if(v<) v*=-;
int id = v%Mod;
while(mp[id] != inf){
if(mp[id]==u) return true;
id++;
id%=Mod;
}
return false;
}
int main()
{
inf = inf*inf;//这里超int也没有关系,因为开始定义的int不够大
int n1 , n2 , n3 ,cas = ;
while(~scanf("%d%d%d",&n1,&n2,&n3))
{
for(int i = ; i < n1; i++) scanf("%lld",&a[i]);
for(int i = ;i < n2 ; i++) scanf("%lld",&b[i]);
for(int i = ;i < n3; i++) scanf("%lld",&c[i]);
int cc = ;
for(int i = ;i < n1; i++)
for(int j = ; j < n2 ; j++)
d[cc++] = a[i]+b[j];
sort(d,d+cc);
int n = unique(d,d+cc)-d;
for(int i = ; i < Mod ;i++) mp[i] = inf;
for(int i = ;i < n; i++) insert(d[i]);
ll s;
scanf("%lld",&s);
printf("Case %d:\n",cas++);
for(int i = ;i < s ;i++){
ll x;
scanf("%lld",&x);
bool flag = false;
for(int j= ; flag == false&&j<n3;j++)
if(find(x-c[j]))flag = true;
if(flag) puts("YES");
else puts("NO");
}
}
return ;
}

Can you find it?(哈希)的更多相关文章

  1. [PHP内核探索]PHP中的哈希表

    在PHP内核中,其中一个很重要的数据结构就是HashTable.我们常用的数组,在内核中就是用HashTable来实现.那么,PHP的HashTable是怎么实现的呢?最近在看HashTable的数据 ...

  2. java单向加密算法小结(2)--MD5哈希算法

    上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...

  3. Java 哈希表运用-LeetCode 1 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. 网络安全——Base64编码、MD5、SHA1-SHA512、HMAC(SHA1-SHA512)哈希

    据说今天520是个好日子,为什么我想起的是502.500.404这些?还好服务器没事! 一.Base64编码 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之 ...

  5. Oracle 哈希连接原理

    <基于Oracle的sql优化>里关于哈希连接的原理介绍如下: 哈希连接(HASH JOIN)是一种两个表在做表连接时主要依靠哈希运算来得到连接结果集的表连接方法. 在Oracle 7.3 ...

  6. SQL连接操作符介绍(循环嵌套, 哈希匹配和合并连接)

    今天我将介绍在SQLServer 中的三种连接操作符类型,分别是:循环嵌套.哈希匹配和合并连接.主要对这三种连接的不同.复杂度用范例的形式一一介绍. 本文中使用了示例数据库AdventureWorks ...

  7. BZOJ 3555: [Ctsc2014]企鹅QQ [字符串哈希]【学习笔记】

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2046  Solved: 749[Submit][Statu ...

  8. [bzoj3207][花神的嘲讽计划Ⅰ] (字符串哈希+主席树)

    Description 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快!J要逆袭了!” …… 描述 这一天D ...

  9. minHash最小哈希原理

    minHash最小哈希原理 收藏 初雪之音 发表于 9个月前 阅读 208 收藏 9 点赞 1 评论 0 摘要: 在数据挖掘中,一个最基本的问题就是比较两个集合的相似度.通常通过遍历这两个集合中的所有 ...

  10. .net的一致性哈希实现

    最近在项目的微服务架构推进过程中,一个新的服务需要动态伸缩的弹性部署,所有容器化示例组成一个大的工作集群,以分布式处理的方式来完成一项工作,在集群中所有节点的任务分配过程中,由于集群工作节点需要动态增 ...

随机推荐

  1. awk 字符拼接

    i-6gdkkemx 2 2 #cat cpu.txt | awk '{print $1,$2"核"$3"G"}' i-6gdkkemx 2核2G

  2. 谈谈CommonsChunkPlugin抽取公共模块

    引言 webpack插件CommonsChunkPlugin的主要作用是抽取webpack项目入口chunk的公共部分,具体的用法就不做过多介绍,不太了解可以参考webpack官网介绍: 该插件是we ...

  3. The Hungarian Abhorrence Principle

    原文:http://www.butunclebob.com/ArticleS.UncleBob.TheHungarianAbhorrencePrinciple    The Hungarian Abh ...

  4. java方向及学习方法

    随笔:由于回首最近刚刚上班的缘故,平时基本没时间上播客了,所以回首会定期的抽时间分享一些干货给朋友们,就是周期不会像之前那么频繁了.最近有朋友跟回首说想没事儿的时候自学Java,但苦于不知道怎么去学, ...

  5. springBoot系列教程06:参数验证及验证信息国际化

    在springboot应用中要验证参数是否正确很简单,web应用已经包含了validation的 1.定义需要被验证的参数实体,并用注解标明错误类别和错误信息 package com.xiao.dom ...

  6. Mysql中字符集总结

    有时候,在Mysql数据库中会经常遇到乱码的问题,现在普遍的做法就是全部强行把编码格式都设置成utf8模式,就可以解决这个问题,以前是知其然,不知其所以然,今天我就稍微研究了下Mysql的字符集. 就 ...

  7. php gif处理

    http://www.jb51.net/article/51702.htm https://github.com/Sybio/GifCreator https://github.com/Sybio/G ...

  8. 如何高逼格读取Web.config中的AppSettings

    http://edi.wang/post/2015/4/22/how-to-read-webconfig-appsettings-with-bigiblity 先插句题外话,下版本的ASP.NET貌似 ...

  9. Building an MFC project for a non-Unicode character set is deprecated

    1>------ 已启动生成: 项目: TestSdk, 配置: Debug Win32 ------1>C:\Program Files (x86)\MSBuild\Microsoft. ...

  10. vue-devtools(vue 2.0)手动安装与使用 ? 如何处理Vue.js is detected on this page ?

    vue-devtools手动安装与使用   一.在github上下载压缩包,github下载地址:https://github.com/vuejs/vue-devtools 二.解压到本地的某盘 三. ...