由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - counting quickperm algorithm
相关主题
Non-recursive permutationT家电面面经并且不解为何被秒拒
关于permutation和combinationString permunation question (CS)
Interview Questionreverse an array
一个容易记忆的permutation算法iterative string permutation
Given a string, find all its permutations without any repetition?一道题:number of permutation是 for a total score
如何写内存速度最优化的string permutation?有重复字符How to handle the return type of container.size() in C++
今天才整明白Permutation的最优解!?问题:Find the minimum number of "swaps" needed to sort an array
BB onsite惨败而归 血的教训!请教 怎样存下这个string
相关话题的讨论汇总
话题: index话题: quickperm话题: display话题: tmp话题: swap
进入JobHunting版参与讨论
1 (共1页)
s**x
发帖数: 7506
1
this is used to do permutation without recursion.
哪位大侠能解释一下如何理解 这两句?
// IF i is odd then j = p[i] otherwise j = 0
// swap(a[j], a[i])
这个是算法的核心, 想了半天也不理解, 算法当然是对的。

http://www.freewebs.com/permute/quickperm.html
thanks!!!
// NOTICE: Copyright 2008, Phillip Paul Fuchs
#define N 12 // number of elements to permute. Let N > 2
void QuickPerm(void) {
unsigned int a[N], p[N];
register unsigned int i, j, tmp; // Upper Index i; Lower Index j
for(i = 0; i < N; i++) { // initialize arrays; a[N] can be any type
a[i] = i + 1; // a[i] value is not revealed and can be arbitrary
p[i] = 0; // p[i] == i controls iteration and index boundaries
for i
}
//display(a, 0, 0); // remove comment to display array a[]
i = 1; // setup first swap points to be 1 and 0 respectively (i & j)
while(i < N) {
if (p[i] < i) {
j = i % 2 * p[i]; // IF i is odd then j = p[i] otherwise j = 0
tmp = a[j]; // swap(a[j], a[i])
a[j] = a[i];
a[i] = tmp;
//display(a, j, i); // remove comment to display target array a[]
p[i]++; // increase index "weight" for i by one
i = 1; // reset index i to 1 (assumed)
} else { // otherwise p[i] == i
p[i] = 0; // reset p[i] to zero
i++; // set new index value for i (increase by one)
} // if (p[i] < i)
} // while(i < N)
} // QuickPerm()
1 (共1页)
进入JobHunting版参与讨论
相关主题
请教 怎样存下这个stringGiven a string, find all its permutations without any repetition?
问一道g电面题如何写内存速度最优化的string permutation?有重复字符
问道面试题今天才整明白Permutation的最优解!?
permutation sequence怎么解?BB onsite惨败而归 血的教训!
Non-recursive permutationT家电面面经并且不解为何被秒拒
关于permutation和combinationString permunation question (CS)
Interview Questionreverse an array
一个容易记忆的permutation算法iterative string permutation
相关话题的讨论汇总
话题: index话题: quickperm话题: display话题: tmp话题: swap