permutation

Are there any better methods to do permutation of string?

守給你的承諾、 提交于 2019-12-17 04:45:47
问题 void permute(string elems, int mid, int end) { static int count; if (mid == end) { cout << ++count << " : " << elems << endl; return ; } else { for (int i = mid; i <= end; i++) { swap(elems, mid, i); permute(elems, mid + 1, end); swap(elems, mid, i); } } } The above function shows the permutations of str (with str[0..mid-1] as a steady prefix, and str[mid..end] as a permutable suffix). So we can use permute(str, 0, str.size() - 1) to show all the permutations of one string. But the function

Regex to match all permutations of {1,2,3,4} without repetition

感情迁移 提交于 2019-12-17 04:35:19
问题 I am implementing the following problem in ruby. Here's the pattern that I want : 1234, 1324, 1432, 1423, 2341 and so on i.e. the digits in the four digit number should be between [1-4] and should also be non-repetitive. to make you understand in a simple manner I take a two digit pattern and the solution should be : 12, 21 i.e. the digits should be either 1 or 2 and should be non-repetitive. To make sure that they are non-repetitive I want to use $1 for the condition for my second digit but

Regex to match all permutations of {1,2,3,4} without repetition

坚强是说给别人听的谎言 提交于 2019-12-17 04:35:11
问题 I am implementing the following problem in ruby. Here's the pattern that I want : 1234, 1324, 1432, 1423, 2341 and so on i.e. the digits in the four digit number should be between [1-4] and should also be non-repetitive. to make you understand in a simple manner I take a two digit pattern and the solution should be : 12, 21 i.e. the digits should be either 1 or 2 and should be non-repetitive. To make sure that they are non-repetitive I want to use $1 for the condition for my second digit but

Shuffle list, ensuring that no item remains in same position

匆匆过客 提交于 2019-12-17 04:32:27
问题 I want to shuffle a list of unique items, but not do an entirely random shuffle. I need to be sure that no element in the shuffled list is at the same position as in the original list. Thus, if the original list is (A, B, C, D, E), this result would be OK: (C, D, B, E, A), but this one would not: (C, E, A, D, B) because "D" is still the fourth item. The list will have at most seven items. Extreme efficiency is not a consideration. I think this modification to Fisher/Yates does the trick, but

C# Permutation of an array of arraylists?

[亡魂溺海] 提交于 2019-12-17 04:31:30
问题 I have an ArrayList[] myList and I am trying to create a list of all the permutations of the values in the arrays. EXAMPLE: (all values are strings) myList[0] = { "1", "5", "3", "9" }; myList[1] = { "2", "3" }; myList[2] = { "93" }; The count of myList can be varied so its length is not known beforehand. I would like to be able to generate a list of all the permutations similar to the following (but with some additional formatting). 1 2 93 1 3 93 5 2 93 5 3 93 3 2 93 3 3 93 9 2 93 9 3 93 Does

C# Permutation of an array of arraylists?

这一生的挚爱 提交于 2019-12-17 04:31:13
问题 I have an ArrayList[] myList and I am trying to create a list of all the permutations of the values in the arrays. EXAMPLE: (all values are strings) myList[0] = { "1", "5", "3", "9" }; myList[1] = { "2", "3" }; myList[2] = { "93" }; The count of myList can be varied so its length is not known beforehand. I would like to be able to generate a list of all the permutations similar to the following (but with some additional formatting). 1 2 93 1 3 93 5 2 93 5 3 93 3 2 93 3 3 93 9 2 93 9 3 93 Does

Permutation algorithm without recursion? Java

青春壹個敷衍的年華 提交于 2019-12-17 04:21:47
问题 I would like to get all combination of a number without any repetition. Like 0.1.2, 0.2.1, 1.2.0, 1.0.2, 2.0.1, 2.1.0. I tried to find an easy scheme, but couldn't. I drew a graph/tree for it and this screams to use recursion. But I would like to do this without recursion, if this is possible. Can anyone please help me to do that? 回答1: Here is a generic permutation enumerator I wrote a year ago. It can also produce "sub-permutations": public class PermUtil <T> { private T[] arr; private int[]

Permutation algorithm without recursion? Java

对着背影说爱祢 提交于 2019-12-17 04:21:04
问题 I would like to get all combination of a number without any repetition. Like 0.1.2, 0.2.1, 1.2.0, 1.0.2, 2.0.1, 2.1.0. I tried to find an easy scheme, but couldn't. I drew a graph/tree for it and this screams to use recursion. But I would like to do this without recursion, if this is possible. Can anyone please help me to do that? 回答1: Here is a generic permutation enumerator I wrote a year ago. It can also produce "sub-permutations": public class PermUtil <T> { private T[] arr; private int[]

Generate a random derangement of a list

喜夏-厌秋 提交于 2019-12-17 04:07:06
问题 How can I randomly shuffle a list so that none of the elements remains in its original position? In other words, given a list A with distinct elements, I'd like to generate a permutation B of it so that this permutation is random and for each n , a[n] != b[n] e.g. a = [1,2,3,4] b = [4,1,2,3] # good b = [4,2,1,3] # good a = [1,2,3,4] x = [2,4,3,1] # bad I don't know the proper term for such a permutation (is it "total"?) thus having a hard time googling. The correct term appears to be

std::next_permutation Implementation Explanation

做~自己de王妃 提交于 2019-12-17 03:21:46
问题 I was curious how std:next_permutation was implemented so I extracted the the gnu libstdc++ 4.7 version and sanitized the identifiers and formatting to produce the following demo... #include <vector> #include <iostream> #include <algorithm> using namespace std; template<typename It> bool next_permutation(It begin, It end) { if (begin == end) return false; It i = begin; ++i; if (i == end) return false; i = end; --i; while (true) { It j = i; --i; if (*i < *j) { It k = end; while (!(*i < *--k))