Peter的PAIP书中包含了这个作为排列问题的解决方案(为了简洁起见,删除了一些部分)
(defun permutations (bag)
;; If the input is nil, there is only one permutation:
;; nil itself
(if (null bag)
'(())
;; Otherwise, take an element, e, out of the bag.
;; Generate all permutations of the remaining elements,
我编写了一个简单的算法来返回字符串的所有可能排列的列表,如下所示:
def get_permutations(sequence):
'''
Enumerate all permutations of a given string
sequence (string): an arbitrary string to permute. Assume that it is a
non-empty string.
Returns: a list of all permutations of sequence
'''
if len(sequence)
我试图理解生成数组排列的算法的时间和空间复杂性。给定一个部分构建的排列,其中已经从k元素中选择了n元素,该算法从其余的n-k元素中选择元素k+1,并调用自己来选择其余的n-k-1元素:
public static List<List<Integer>> permutations(List<Integer> A) {
List<List<Integer>> result = new ArrayList<>();
permutations(A, 0, result);
return result;
我是一个程序员,我做了一些C#,perl和python,总之,我找到了这个递归代码来生成一系列符号和字母的排列,但是我不知道它是如何工作的?有谁愿意解释一下吗?
#!/usr/bin/env python
#-*- coding:utf-8 -*-
def generate(charlist,state,position):
for i in charlist:
state[position] = i
if position == (len(state)-1):
print "".join(state)
我想要得到字符串排列的具体组合,如字母表。为了理解我,我将向您展示我使用的代码:
public class PermutationExample {
public static List<String> getPermutation(String input) {
List<String> collection = null;
if (input.length() == 1) {
collection = new ArrayList<String>();
collection.add(input);
我有一个程序,可以打印给定字符串的所有可能的排列。但它是静态的,我硬编码了3 for循环,假设字符串的长度为3。但如果我必须使其动态,用户可能会输入长度为5的字符串。所以我想问的是,我是否可以根据用户的输入注入for循环?
import java.util.*;
import java.lang.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
我有个排序算法。我知道它可以用许多其他更简单的方式写出来,但这不是我问题的重点。
这是算法
sort(A : Array of N, i : N, j : N)
assert j-i+1 isTwoPotency
if A[i] > A[j] then swap A[i] and A[j]
if i+1 < j then
k:= (j − i + 1)/4
sort(A, i, j − 2k)
sort(A, j − 2k + 1, j)
sort(A, i + k, j − k)
sort(A, i, j − 2k)
sort(A,
我需要证明Heap生成排列算法的正确性。它的伪码如下:
HeapPermute(n)
//Implements Heap’s algorithm for generating permutations
//Input: A positive integer n and a global array A[1..n]
//Output: All permutations of elements of A
if n = 1
write A
else
for i ←1 to n do
HeapPermute(n − 1)
if n is odd
swap A[1]
我正在尝试解决一个动态编程问题,部分问题涉及找到一组'p‘数的排列数,这些排列数加起来将是一个数'n’。P个数集中的每个数应介于0到n之间(包括0和n)。
例如,如果n=4,p= 3,我有以下12种排列
{4,0,0},{0,4,0},{0,0,4}
{2,2,0},{2,0,2},{0,2,2}
{1,3,0},{1,0,3},{0,1,3}
{1,1,2},{1,2,1},{2,1,1}
我从这个DP方法开始。
n(i,j) represents number of ways of representing n using i,j in p positions
我的基本情
def permutationString(word):
print('word', word)
result = []
if len(word) == 0:
result.append('')
return result
for i in range(len(word)):
before = word[0 : i]
after = word[i + 1 :]
print('before',before)
print
我使用下面的代码来使用OpenMP任务构造来查找数组元素的和。
在n = 10000.之前,代码将产生正确的结果
但除此之外,我还遇到了一个分割错误。使用gdb,,我发现错误发生在对reduce()的一个递归调用中。输入数组分配没有问题,我已经验证了这一点。
有人对这个问题有什么建议吗?
int reduce (int *arr, unsigned long int n)
{
int x;
if (n <= 0)
return 0;
#pragma omp parallel
{
#pragma omp sing
整个夏天,我决定写一个程序,通过使用你要输入的单词中所有可能的字母组合来解决字谜问题。我设法做到了,但以这样一种方式,它可以很快算出一个3-4个字母的单词,但如果再多一点,就需要很长时间了!无论如何,在其他网站上请求帮助后,一些男孩/女孩为我写了一些代码来解决我的问题。
当时我并不理解它,即使它已经被注释过了,今天我又看了一眼,看看能不能从中得到任何东西,但遗憾的是,什么都没有。
我已经研究了大约2个小时的排列,但我一无所获--代码在互联网上,但没有人解释得足够好(或足够简单),让我能够理解这个概念。我将在下面发布我尚未理解的代码,如果有人能详细解释这一点以及它是如何工作的,那就太好了!
Pu
我要做的是创建一组字符串的排列,其中包含字符串中的'['和']'。然后,只将create_perm创建的字符串添加到一个新集合中,该字符串包含一个'['作为第一个,']'作为最后一个。
我所做的:
def create_perm(lst_str):
if len(lst_str) <= 1:
return {lst_str}
old_set = set()
for idx, c in enumerate(lst_str):
for perm in create_perm
给定一个2D字符数组,找到从左上角到右下角的所有可能路径。我有以下递归解决方案。有人能解释一下如何找到它的复杂性吗?还有,有没有更好的解决方案呢?我不太熟悉动态编程,但我认为它可以以某种方式解决这个问题。
public ArrayList<ArrayList<Character>> getPaths(char [][]grid){
return getPaths(grid, 0, 0, new ArrayList<Character>());
}
public ArrayList<ArrayList<Ch
对于Project Euler问题24,我有以下(正确的)解决方案。我对Python比较陌生,在Python的几个要点上遇到了困难。
首先,代码:
# A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4.
# If all of the permutations are listed numerically or alphabetically, we call it lexicographic o
现在,我的程序花费了超过10分钟LOL尝试显示所有可能的单词(如果这些单词在文件中),可以从给定的字母创建。在该文件中,包含了更多的4000+语句:如何通过使用递归使程序运行得更快,而不使用任何库,因为我是新手。
if user input letters: b d o s y
然后,它将在该文件中查找所有可能的单词以创建:
b
d
boy
boys
by
代码:
words = set()
def found(word, file):
## Reads through file and tries
## to match given word in a line.