求一个简单的算法

yuxi214 2009-04-25 01:22:37
有N个数组 假设 20<N<500
每个数组有M个 对象假设 10<M<5000
找出这N个数组重复的对象
求一个简捷的算法.无所谓什么语言,只要提供一个思路就可以了
...全文
134 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
only_lonely 2009-04-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 LemIST 的回复:]
Sorry, my system has no chinese input. Am installing one.
[/Quote]

you are ox man !
LemIST 2009-04-25
  • 打赏
  • 举报
回复
Sorry, my system has no chinese input. Am installing one.
yuxi214 2009-04-25
  • 打赏
  • 举报
回复
大哥,麻烦翻译下,看老累的
热学沸腾56 2009-04-25
  • 打赏
  • 举报
回复
学习..帮顶!
LemIST 2009-04-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 shrinerain 的回复:]
如果你的内存足够的话, 那么使用hash表最快的.

比如: 假设你有N个数组, 叫arr1,arr2...arrN

for(int i=0;i < arr1.length;i++)
{
hash[arr1[i]] = hash[arr1[i]] | 1 < <1;
}

for(int i=0;i < arr2.length;i++)
{
hash[arr2[i]] = hash[arr2[i]] | 1 < <2;
}
...

最后只需要查看一下hash表, 找出value值为2^n就可以了.

这个复杂度为O(N).


如果你内存不是很够, 那么先排序, 然后遍历一遍也可以了.


[/Quote]

Good! Nice idea.
LemIST 2009-04-25
  • 打赏
  • 举报
回复
Step 1:
write a class to wrap such objects, like this
class Wrapper
{
object obj;
int arrayIndex; // Store which array such object belongs to
}

Step 2:
Define its comparing rules by implementing IComparable<T> interface.
Comparing Rule: Compare by Wrapper.obj.

Step 3:
Read from those arrays, make the wrapper for them. Store those objects in a ArrayList.

Step 4:
Sort the ArrayList. ArrayList.Sort().

Step 5:
Count each object one by one.
If the number of certain object is >= N, Then check whether these objects belongs to each array by checking those Wrapper's arrayIndex.
If so, then it is target you want to find.
shrinerain 2009-04-25
  • 打赏
  • 举报
回复
如果你的内存足够的话, 那么使用hash表最快的.

比如: 假设你有N个数组, 叫arr1,arr2...arrN

for(int i=0;i< arr1.length;i++)
{
hash[arr1[i]] = hash[arr1[i]] | 1<<1;
}

for(int i=0;i< arr2.length;i++)
{
hash[arr2[i]] = hash[arr2[i]] | 1<<2;
}
...

最后只需要查看一下hash表, 找出value值为2^n就可以了.

这个复杂度为O(N).


如果你内存不是很够, 那么先排序, 然后遍历一遍也可以了.

while(i< arr1.length && j< arr2.length)
{
if(arr1[i]< arr2[j])
{
i++;
}
...
}


考虑先将两个最小的数组排序, 并遍历, 得出共同数组, 共同数组再和第三小的数组排序并遍历.

以此和更大的数组比较.

你用快排, 这样算法是O(nlogN). 但可以节约一些内存.


在实际工作中, 你用Linux下的diff命令, 或者windows下的fc命令, 简单几行脚本就能实现你的要求.
yuxi214 2009-04-25
  • 打赏
  • 举报
回复
找出几个数组都包含的项,无序
风骑士之怒 2009-04-25
  • 打赏
  • 举报
回复
up,是找出每个数组里面重复的对象,还是全部查找
shrinerain 2009-04-25
  • 打赏
  • 举报
回复
你的数组是否有序?

是要求找出所有数组都包含的项还是有两个或两个以上数组都包含的项?

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧