JAVA动态规划(四)--根据给定0和1的个数,求字典序排在第K位的数【微软笔试题】

题目:
Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 256MB
Description
Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s.
Write a program to find and output the K-th string according to the dictionary order. If s​uch a string doesn’t exist,
or the input is not valid, please output “Impossible”. For example, if we have two ‘0’s and two ‘1’s,
we will have a set with 6 different strings, {0011, 0101, 0110, 1001, 1010, 1100}, and the 4th string is 1001.
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10000),
the number of test cases, followed by the input data for each test case.
Each test case is 3 integers separated by blank space: N, M(2 <= N + M <= 33 and N , M >= 0),
K(1 <= K <= 1000000000). N stands for the number of ‘0’s, M stands for the number of ‘1’s,
and K stands for the K-th of string in the set that needs to be printed as output.
Output
For each case, print exactly one line. If the string exists, please print it, otherwise print “Impossible”.
Sample In
3
2 2 2
2 2 7
4 7 47
Sample Out
0101
Impossible
01010111011
简单来说就是:给定n个 0和m 个1,求字典排序的第 k个值。【这是微软MicroSoft之前校招的一道笔试题】

分析:用d[i][j]表示由i个0,j个1最能能够组合的数,则状态转移方程为:d[i][j]=d[i-1][j]+d[i][j-1];

如果最高位是0的组合数大于等于K(dp[n][m-1] >= K), 那么最高为一定为0。
如果最高位是0的组合数小于K, 那么最高位一定为1。 此时,应该减去最高位为0的前一半的数,最高位为0的组合有dp[n-1][m]个,问题变成:在N个0和(M-1)个1中需要为(K-dp[n][m-1])的数字是多少。

package dynamic_programming;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
 * @author Gavenyeah
 * @date Time: 2016年4月5日下午5:05:10
 * @des: 
 */
//d[i][j]表示由i个0,j个1最能能够组合的数
//则状态转移方程为:d[i][j]=d[i-1][j]+d[i][j-1];
public class FindKthString {
    Listset=new ArrayList();

    public static void main(String[] args) {
        FindKthString te=new FindKthString();
        te.getString();
        te.printSet();
    }
    public void getString(){ //多组输入,输入结束了,一次输出
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        for(int r=0;rint n=in.nextInt();
            int m=in.nextInt();
            int k=in.nextInt();
            int dp[][]=getDpArray(n, m);
            findKString(n,m,k,dp);
        }
        in.close();
    }

    public void findKString(int n,int m,int k,int d[][]){ //找到排在第k位上的数
        StringBuffer s=new StringBuffer();
        if(k>d[n][m]){
            set.add("Impossible");
        }else{
            while(k>0&&n>0){
                if(d[n-1][m]>=k){
                    s.append("0");
                    n--;
                }else {
                    s.append("1");
                    k=k-d[n-1][m];//最高位为1时,减去前面的最高位为0 的数
                    m--;//此时1的个数减1
                }
            }
            while(m>0){
                s.append("1");
                m--;
            }
            set.add(s.toString());
        }
    }

    public int[][] getDpArray(int n,int m){
        int[][]d=new int[n+1][m+1];
        for(int i=1;i<=n;i++){
            d[i][0]=1;
        }
        for(int j=1;j<=m;j++){
            d[0][j]=1;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                d[i][j]=d[i-1][j]+d[i][j-1];
            }
        }
        return d;
    }
    public void printSet(){
        while(!set.isEmpty()){
            System.out.println(set.remove(0));
        }
    }
    /*public void findKString(int n,int m,int k){
        int len=m+n;
        int[]a=new int[len];
        for(int i=0;iset.size()){
            list.add("Impossible");
        }else{
            Iteratoriter=set.iterator();
            while(k>1){
                iter.next();
                k--;
            }
            String str=iter.next();
            list.add(str);
        }
        set.clear();
    }
    public void getPermutations(int a[],int p,int q){
        if(p==q){
            String buf=new String();
            for(int j=0;j<=q;j++){
                buf=buf+a[j];
            }
            set.add(buf);
        }
        else{
            for(int i=p;i<=q;i++){
                swap(a,i,p);
                getPermutations(a,p+1,q);
                swap(a,i,p);
            }
        }
    }
    public void swap(int a[],int p,int q){
        int temp=a[p];
        a[p]=a[q];
        a[q]=temp;
    }*/
}

你可能感兴趣的:(动态规划)