intersection set
来源:互联网 发布:sql注入点 编辑:程序博客网 时间:2024/06/08 02:00
intersection set
时间限制:1000 ms | 内存限制:65535 KB
难度:1
- 描述
- 两个集合,你只需要求出两个集合的相同元素,并输出个数。
- 输入
- m n
{a1 , a2 , a3 , a4 ... ai ... am}
{b1 , b2 , b3 , b4 ... bi ... bn}
1 <= n , m <= 50000 , 保证一个集合中不会输入重复数据
0 <= ai , bi <= 100000
多组测试数据 - 输出
- 一行一个数据,为两个集合中相同的元素个数
- 样例输入
8 81 5 6 9 10 12 16 595 6 9 8 15 17 65 98
- 样例输出
3
代码实现:
#include<stdio.h>
#define N 100001
int a[100001];
int main()
{
int m,n,k,p=1,s,i;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<m;i++)
{
scanf("%d",&k);
a[k]=p;
}
for(i=0,s=0;i<n;i++)
{
scanf("%d",&k);
if(a[k]==p)
s++;
}
p++;
printf("%d\n",s);
}
return 0;
}
小结:在本题中,常规思路是用两层for循环来逐个比较实现,但是会超时。在借鉴了别人的思路后发现,这个思路特别好,而且题目中也说明了每个数字只出现一次,为建立一个标记数组打下基础,通过比较对应位置有无标记来比对是否有值相等,这个想法真的很赞,大大节约了求解时间。
阅读全文
0 0
- intersection set
- intersection set
- intersection set
- intersection set
- nyoj-626-intersection set
- NYOJ626 intersection set
- nyoj 643intersection set
- NYOJ intersection set
- 626 intersection set
- 【Lua】ElJudge 002 Set Intersection
- set intersection问题求解(python版)
- EI judge 002 Set Intersection 算法题解
- 349. Intersection of Two Arrays (set)
- Intersection
- Intersection
- Intersection
- Intersection
- LeetCode 349. Intersection of Two Arrays 350. Intersection of Two Arrays II set map使用 ****
- pdf转换cad如何进行转换才最简单呢?
- hive学习笔记
- 基于XML实现AOP
- 【NOIP2017】SummerTraining0710
- Bootstrap基础4——form表单
- intersection set
- Maven 简单使用创建Web项目
- pyhton爬虫(11)——抓取亚马逊商品类别信息
- 使用drawBitmap绘制图片
- 应届生落户上海申请及办理流程
- HDU1330 Deck
- RocketMQ简介
- tipso插件---消息提示框
- Java线程、并发概念整理