繁体   English   中英

存储二进制字符串的所有可能序列,Java

[英]Storing all possible sequences of a binary string, Java

目的 :给定一个二进制字符串作为输入,存储所有可能的组合中将字符串中的所有1都切换为1和0所得的字符串。

示例给定二进制字符串“ 110”,则每个“ 1”值都可以切换为“ 1”或“ 0”。 必须将“ 0”值保留为“ 0”,并且顺序很重要。

字符串011产生:[011,010,001,000]

字符串101产生:[101,100,001,000]

字符串111产生:[111、110、101、100、011、010、001、000]

我面临的问题:给定字符串“ 111”时,我的代码未存储每个对应子序列的所有可能组合。

我的代码输出

Set:110 [110]
Set:011 [011, 010, 001, 000]
Set:000 [000]
Set:111 [111, 110, 101, 100, 011, 010, 001, 000]
Set:100 [100]
Set:001 [001, 000]
Set:101 [101, 100]
Set:010 [010]

很少有没有将所有可能的组合存储到哈希表中的示例:010(不包含000),101(不包含000或001)。

当功能最初被赋予“ 111”作为输入时,111,011,001都具有正确存储的组合。

编码:

public static List<String> subsequence(char [] set, HashMap<String,List<String>> hs, int position){
    List<String> listOfSubsequence = new ArrayList<String>();

    // Dynamic programming part
    if (hs.containsKey(String.valueOf(set))){
        return hs.get(String.valueOf(set));
    }

    // base case
    else if (position >= set.length ){
        listOfSubsequence.add(String.valueOf(set));
        hs.put(String.valueOf(set), listOfSubsequence);
        return listOfSubsequence;
    } 

    else if (set[position] == '0'){
        return(subsequence(set,hs,position + 1));
    }
    else{
        // Last situation where we have a 1 at position we're looking at
        listOfSubsequence.addAll(subsequence(set,hs,position + 1));
        set[position] = '0';
        listOfSubsequence.addAll(subsequence(set,hs,position));
        set[position] = '1';

        hs.put(String.valueOf(set), listOfSubsequence);
        return listOfSubsequence;
    }
}

有位技巧可以枚举给定位掩码的所有子掩码

sub = mask;
while (sub) {
    output sub
    sub = (sub - 1) & mask;
    //clears LSB, sets trailing 0s, removes bits not presenting in mask
}
output sub  ////zero one

对于掩码5 (binary 101)此伪代码给出输出5,4,1,0 (binary 101, 100, 001, 000)

您的实现是正确的。 您可能执行错了。 您应该使用position=0执行子subsequence方法:

char[] set = "010".toCharArray();
HashMap<String, List<String>> hs = new HashMap();
int position = 0;
System.out.println(subsequence(set, hs, position));

上面的输出应为:

[010, 000]

我尝试过,只是将所有结果从0输出到给定的二进制字符串。 跟踪0的pos。 这显然没有效果。 但是您可以根据需要对其进行优化。

printPerm("111");

public static void printPerm(String b)
    {
        double n = Integer.parseInt(b, 2);
        ArrayList<Integer> aList = new ArrayList<>();

        //1.) Notice where the zeros are
        for(int i=0;i<b.length() ;i++)
        {
            if(b.charAt(i) == '0')
            {
                aList.add(i);
            }
        }

        //2. Convert the binary to int
        ArrayList<String> collection = new ArrayList<>();
        String bin ;
        for(int i=0;i<n;i++)
        {
             bin = Integer.toBinaryString(i);
             bin = String.format("%" + b.length() + "s",bin).replace(" ", "0") ;
            Boolean isValid =false;
            if(aList.size() ==0 )
            {
                //No zeros found. Easy
                isValid = true;
            }else
            {
                for(Integer zi : aList)
                {
                    if(bin.charAt(zi) == '0')
                    {
                        isValid = true;
                    }
                    else
                    {
                        isValid = false;
                    }
                }
            }
            if(isValid)
            {
                collection.add(bin);
            }

        }

        //3 Print strings.
        for(String s: collection)
        {
            System.out.println(s);
        }
        System.out.println(b);

    }

到目前为止,解决方案似乎是史诗般的。 这是另一种选择。

import java.util.ArrayList;

public class Experimental {

  static final class BitStringEnumerator {
    final char[] bits;
    final ArrayList<String> results = new ArrayList<>();

    BitStringEnumerator(String bits) {
      this.bits = bits.toCharArray();
      enumerate(0);
    }

    /** Enumerate all strings incorporating bits from position pos. */
    private void enumerate(int pos) {
      if (pos >= bits.length) {
        // All positions have been incorporated. The string is complete.
        results.add(new String(bits));
      } else {
        // The string's not complete yet. Enumerate all strings with this bit as-is.
        enumerate(pos + 1);
        if (bits[pos] == '1') {
          // If this bit is a 1, also enumerate all with it flipped to 0.
          bits[pos] = '0';
          enumerate(pos + 1);
          bits[pos] = '1';
        }
      }
    }
  }

  public static void main(String[] args) {
    System.out.println(new BitStringEnumerator("111").results);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM