0

我正在尝试编写一个函数,它将像“---”这样的输入转换为 000,001,010,011,100,101,110 和 111。另一个示例是“1--”-> 100,101,110,111。到目前为止,这是我的代码,但它只产生了一些解决方案:

static void expandPLA(char[]plaRow){
        boolean sawDontCare=false;
        for(int x = 0; x< plaRow.length; x++){
            if(plaRow[x]=='-'){
                sawDontCare=true;
                plaRow[x]='0';
                expandPLA(plaRow);
                plaRow[x]='1';
                expandPLA(plaRow);
            }
        }
        if(!sawDontCare)
            arrayList.add(plaRow);    
    }

arrayList 保存输出值。任何人都看到有什么问题吗?

4

3 回答 3

2

我为您创建了一个示例实现,它打印出您在上面指出的值列表。当然,您可以做任何您想做的事情来代替打印到控制台:

import java.util.*;
import java.lang.*;

class Main {

    public static void expandPLA(char[] pla) {

        // How many don't cares are we handling
        int empties = 0;
        for (int i = 0; i < pla.length; i++) {
            if (pla[i] == '-') { empties++; }
        }

        // Now we know we're counting from 0 to 2^empties in binary
        for (int j = 0; j < Math.pow(2,empties); j++) {

            // For each value of j we're going to create a new string pattern
            // and fill in each don't care with the correct digit of j
            String pattern = String.copyValueOf(pla);
            String bin = Integer.toBinaryString(j);

            // Pad bin with zeros
            int pad = empties - bin.length();
            for (int z = 0; z < pad; z++) {
                bin = "0" + bin;
            }

            // For each empty spot we're going to replace a single '-' with
            // the next most significant digit
            for (int k = 0; k < empties; k++) {
                char digit = bin.charAt(k);
                pattern = pattern.replaceFirst("-", String.valueOf(digit));
            }

            // We're just going to print this out for now, but you can do
            // whatever it is you want at this point.
            System.out.println(pattern);

        }

    }

    public static void main (String[] args) throws java.lang.Exception {
        Main.expandPLA(new char [] { '1', '-', '-', '1', '-', '1', '-', '-' });
    }

}

注意:我上面的算法可能会收紧很多。我懒于如何用 0 填充我的二进制数,并且可能有比字符串替换更好的方法将我的数字放入无关空格。考虑这是一种概念证明,它可能会更节省内存和时间,但我认为它优于递归。

于 2013-03-20T02:28:51.417 回答
0

如果你真的想要递归,这样的东西应该可以工作:

static final char[] digits = new char[]{'0','1'};

private void expandPLA(char[] plaRow, char[] value, int x) {
    if (x == plaRow.length) {
        arrayList.add(value);
        return;
    }
    if (plaRow[x] == '-') {
        for (char digit : digits) {
            value[x] = digit;
            expandPLA(plaRow, value, x + 1);
        }
    } else {
        value[x] = plaRow[x];
        expandPLA(plaRow, value, x + 1);
    }
}
于 2013-03-20T02:37:08.910 回答
0

它是递归的,但它不是 Java。

(define (expand-pla code)
  (define (cons-of x) (lambda (l) (cons x l)))
  (define cons-1 (cons-of #\1))
  (define cons-0 (cons-of #\0))

  (map list->string
       (let building ((codes  (string->list code)))
         (if (null? codes)
             (list '())
             (let ((rest (building (cdr codes))))
               (case (car codes)
                 ((#\0) (map cons-0 rest))
                 ((#\1) (map cons-1 rest))
                 ((#\-) (append (map cons-0 rest)
                                (map cons-1 rest)))))))))

也许你会觉得它很有趣。而且,它有效:

> (expand-pla "1--")
("100" "101" "110" "111")

这是一个尾递归版本。

(define (expand-pla code)
  (define (cons-of x) (lambda (l) (cons x l)))
  (define cons-1 (cons-of #\1))
  (define cons-0 (cons-of #\0))
  (define (compose f g) (lambda (x) (f (g x))))

  (let building ((codes (string->list code)) (result (list '())))
    (if (null? codes)
        (map (compose list->string reverse) result)
        (building (cdr codes)
                  (case (car codes)
                    ((#\0) (map cons-0 result))
                    ((#\1) (map cons-1 result))
                    ((#\-) (append (map cons-0 result)
                                   (map cons-1 result))))))))
于 2013-03-20T02:45:21.287 回答