题目连接: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. python的属性(property)使用

    在面向对象编程的时候,我们定义一个Person类 class Person: def __init__(self): self.age = 22 这样写法能够方便的访问属性age, p = Perso ...

  2. Cat 跨线程之 TaggedTransaction 用法和原理分析

    代码 package com.dianping.cat.message.internal; import com.dianping.cat.Cat; import com.dianping.cat.m ...

  3. OpenStack运维(一):OpenStack项目和用户

    1.添加项目 keystone tenant-create --name=demo [--description tenant-description --enable false] demo:项目名 ...

  4. webpack配置报错:invalid configuration object.webpack has been initialisted using a configuration objcet that does not match thie API schema

    最近接收了别人的项目,webpack配置总是报错如下:最后找到了解决办法,在此分享一下: 错误情况: 解决办法: 将package.json里面的colors删除掉即可

  5. php逐行读取txt文件写入数组的方法

    使用说明: 采用fopen 方法,逐行读取数据,并使用feof($fp)  判断是否文件截止,最后通过filter() 方法,去除空白行,得到所需数据 $file = fopen("user ...

  6. thinkinginjava学习笔记06_复用类

    MarsEdit粘代码好麻烦,所有代码交给github:https://github.com/lozybean/MyJavaLearning 复用一个类常用的两种方式:组合.继承: 组合 将对象引用置 ...

  7. Vue 爬坑之路(五)—— 组件进阶

    组件(Component)是 Vue.js 最强大的功能之一,之前的文章都只是用到了基本的封装功能,这次将介绍一些更强大的扩展. 一.基本用法 在使用 vue-cli 创建的项目中,组件的创建非常方便 ...

  8. SpringMVC ModelAndView、Map、Model、ModelMap

    目标方法返回值可以是ModelAndView .Map.Model.ModelMap类型,但最根本还都是ModelAndView. 其中可以包含试图和模型信息. SpringMVC 会把ModelAn ...

  9. MyEclipse过期后怎么破解

    方法一:写一个程序生成Subscriptioncode import java.io.*; public class MyEclipseGen { private static final Strin ...

  10. 使用FormatMessage函数编写一个内核错误码查看器

    在编写驱动程序的时候,常用的一个结构是NTSTATUS,它来表示操作是否成功,但是对于失败的情况它的返回码过多,不可能记住所有的情况,应用层有一个GetLastError函数,根据这个函数的返回值可以 ...