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

时间:2016-08-15 03:08:56

标签: java algorithm dynamic-programming

目的:给定二进制字符串作为输入,存储通过将字符串中的所有1切换为所有可能组合中的1和0而产生的字符串。

示例给定二进制字符串" 110",每个" 1"值可以切换为" 1"或" 0"。 " 0"值必须保留为" 0"和订购事宜。

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

String 101产生: [101,100,001,000]

String 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,011,001在最初给出功能时都具有正确存储的组合" 111"作为输入。

代码:

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;
    }
}

3 个答案:

答案 0 :(得分:3)

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

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)

答案 1 :(得分:1)

您的实施是正确的。你可能错误地执行了它。您应该使用subsequence执行position=0方法:

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

上述输出应为:

[010, 000]

答案 2 :(得分:0)

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

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);
  }
}