EI judge 002 Set Intersection 算法题解

Set Intersection

Time limit = 5 second(s)

Memory limit = 33000 Kb

Your task is to find the intersection of two sets of positive integers.

Input The input consists of two sets of positive integers A={a1, a2, ..., an} and B={b1, b2, ..., bk} represented as two whitespace-separated lists of numbers. Each list ends with -1, that serves as an end-of-set marker. Repetitions of elements are possible in the input, but not in the output. You may assume that 0 < ai, bi < 106 and that the sets A and B are of size less than 1000.

Output is list of numbers or word "empty" if intersection is empty.

更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

注意地方:

1 防止重复 -- 这里使用set,第二次遍历数组的时候,记得把set重的数据erase掉

2 输出格式这个oj也很严格的,不能多一个空格

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;  

int A[1000] = {0};  

void SetIntersection()
{
    set<int> si;
    int a = 0;
    while (cin>>a && -1 != a)
    {
        si.insert(a);
    }  

    int j = 0;
    while (cin>>a && -1 != a)
    {
        if (si.count(a))
        {
            A[j++] = a;
            si.erase(a);
        }
    }
    if (!j) cout<<"empty";
    else
    {
        j--;
        for (int i = 0; i < j; i++)
        {
            cout<<A[i]<<' ';
        }
        cout<<A[j];//原来要防止多输出一个' '
    }
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索include
, oj
, of
, The
, that
sets
eikr 002、eikr002佐々木あき、intersection、python intersection、set intersection,以便于您获取更多的相关知识。

时间: 2024-08-01 03:43:26

EI judge 002 Set Intersection 算法题解的相关文章

Codeforces B. Taxi 算法题解

简单总结题意: 有一个数组,数组中的数值不会大于4,即只能是1,2,3,4,要把这些数值装进一个容量最大为4的容器里面,使得所用到这样的容器的个数最小. 经测试数据很大,会有10万个数据,所以这里我并不用排序数据,而是使用counting sort的思想,根据特定的数据优化,使得题解时间复杂度为O(n). 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ 程序如下: #include <iostream>

SPOJ Transform the Expression 逆波兰式算法题解

把带括号的一般正常的算式,转换成逆波兰式. 输入和输出如下例子: Input: 3 (a+(b*c)) ((a+b)*(z+x)) ((a+t)*((b+(a+c))^(c+d))) Output: abc*+ ab+zx+* at+bac++cd+^* 总结规律是很困难的事情,总结好就能很快解决: 1 字母直接放结果 2  '('入栈 3 ')' 出栈到'(',然后出栈'(' 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Program

code chef:Divide the Tangerine 橘子分块算法题解

Once Chef decided to divide the tangerine into several parts. At first, he numbered tangerine's segments from1 to n in the clockwise order starting from some segment. Then he intended to divide the fruit into several parts. In order to do it he plann

SPOJ 查找下一个回文Palindrome 算法题解

给出一个数值,well,其实不是数值了,而是一大串数字, 比如 98237482340328490328490324893024,非常长的数字. 找出下一个Palindrome,这个Palindrome在数值上要比当前数值大(不能等于). 如: 9 9 9 9->1 0 0 0 1 1 2 3 4 5 ->1 2 4 2 1 本算法时间效率是O(n),其中n是指数值的位数,不是数值,比如123456789,只有9位,那么本算法接近常数: 更多精彩内容:http://www.bianceng.c

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

(算法导论习题解problem2.4)寻找一个序列中逆序对的数量

一个序列的逆序对是这样的两个元素, 对于序列A而言, i>j且A[i]<A[j], 于是A[i]和A[j]就形成一个逆序对. 研究一个序列中逆序对的数量是有实际意义的, 对于插入排序而言, 它排序的时间与待排序序列的逆序对数量成正比. 下面给出求出一个序列中逆序对数量的算法,类似于归并排序中使用的分治算法:一个序列的逆序对数量为它的左半部分逆序对的数量,加上右半部分逆序对的数量, 最后在合并的时候左半部分元素大于右半部分元素的数量.这几乎和归并算法的过程一模一样,只是在归并的时候加入了计数的操

(算法导论习题解exercise2.3-4)递归版插入排序

没什么可以多说的,直接看代码吧: #include <stdio.h> void display(int array[], int size) { int i; for (i = 0; i < size; ++i) { printf("%d ", array[i]); } printf("\n"); } void insert_sort(int array[], int size) { if (size > 1) { insert_sort(

喜欢算法的来帮帮忙 这道题实在是不会了

问题描述 喜欢算法的来帮帮忙 这道题实在是不会了 地址 :http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1005&cid=29617 麻烦贴下代码就行 解决方案 http://zhidao.baidu.com/link?url=zwfY7XXK0ltMcUGzCgW1L-rBh9EdLp60MP9wKcdTYik9vXGCYKqVVZ-8YACrZWOPYoHZoGm6skoRnzsK8QDg3KTPWoKV9F6Qk_fwCbSyF

几个分布式基础算法

研究生期间分布式课程的小结吧.... 中间件在分布式系统中的地位和角色 为了使种类各异的计算机和网络都呈现为单个的系统,分布式系统常常通过一个"软件层"组织起来,该层在逻辑上位于由用户和应用程序组成的高层与由操作系统组成的低层之间,这样的分布式系统又称为中间件.中间件层延伸到了多台机器上,且为每个应用程序提供了相同的接口.它的重要的目的是提供一定程度的透明性,也就是一定程度上向应用程序隐藏数据处理的分布性.中间件集分布式操作系统与网络操作系统的优点于一身,既能够具有网络操作系统的可扩展