最小的检查,以找到一个列表重复 [英] minimal checks to find repeats in a list
问题描述
给定一个序列(D1,D2,...,DN),我要评估产品,谢谢!(1 - DIJ)对于所有的i = j的,其中DIJ = 1如果DI = DJ,否则为0。
在code,我只有检查的时候DIJ我
PROD = 1;
的for(int i = 1;我n种; ++ I){
对于(INT J =; J< = N; ++ j)条{
督促* =(1 - DIJ);
}
}
我知道当我得到DIJ = 1我管不了,但我想要做的就是的DIJ的最小离pression检查。这样,我有一个EX pression,然后我就可以使用不同的序列,并对其进行评估。所以,我知道我能做到 I<!第j
,而不是 I = j的
。所以,我想扩大了该产品,并得到这样的事情对于n = 3:
(1 - D12)(1 - D13)(1 - D23)= 1 - D12 - D13 - D23 + D12 * D13 + D12 * D23 + D13 * D23 - D12 * D13 * D23
但是,还有更多,我可以做。这当然pression实际上总是等于
1 - D12 - D13 - D23 + 3 * D12 * D13 - D12 * D13 * D23
我的问题是:
-
为什么D12 * D13 = D12 * D23?这始终是真实的(意思是无所谓对D顺序是什么),但我真的不知道为什么,因为在我看来,这意味着D13 = D23这并不总是正确的(这取决于在D序列) 。这是有助于使EX pression小的关系。
-
我如何才能找到像这样的所有关系,并得到一个最小的前pression?是前pression上面最小?我甚至不知道。
您正试图确定是否D包含任何重复或没有。最后,需要你每个条目与对方,这仅仅是列举两种元素的所有独特的组合进行比较。这最终被 N *(N-1)/ 2
。你可以做的更好,首先分拣D和再寻找重复的相邻的一对(一点点 O(N *日志(N)
),或者,假设你坚持有界整数的范围内,就可以将其降低到线性时间有位向量,或者如果你喜欢冒险,一个基数排序。
Given a sequence (d1, d2, ..., dn), I want to evaluate the product (1 - Dij) for all i != j where Dij = 1 if di = dj and 0 otherwise.
The code I have only checks Dij when i
prod = 1;
for (int i=1; i<n; ++i) {
for (int j=i; j<=n; ++j) {
prod *= (1 - Dij);
}
}
I know I can stop when I get Dij=1, but what I'm trying to do is get a minimal expression of the Dij's to check. This way I have one expression and then I can use difference sequences and evaluate it. So I know that I can do i<j
instead of i != j
. So I want to expand out this product and get something like this for n=3:
(1 - D12) (1 - D13) (1 - D23) = 1 - D12 - D13 - D23 + D12*D13 + D12*D23 + D13*D23 - D12*D13*D23
But there is more that I can do. This expression is actually always equal to
1 - D12 - D13 - D23 + 3 * D12*D13 - D12*D13*D23
My questions are:
Why is D12 * D13 = D12 * D23? This is always true (meaning it doesn't matter what the d sequence is) but I don't really get why because it seems to me that this means D13 = D23 which isn't always true (it depends on the d sequence). This is the relation that helps make the expression smaller.
How can I find all the relations like this and get a minimal expression? Is the expression above minimal? I don't even know.
You are trying to determine if D contains any duplicates or not. Ultimately, that requires you to compare every entry with each other, which is simply enumerating all unique combinations of two elements. That ends up being N*(N-1)/2
. You can do a little better by sorting D first and then searching for duplicate adjacent pairs (O(N*log(N)
), or, assuming you are sticking to a bounded range of integers, you can reduce it to linear time with a bit vector, or if you are feeling adventurous, a radix sort.
这篇关于最小的检查,以找到一个列表重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!