微软2014实习生在线测试之K-th string

问题描述:

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

基本思路:

简单点就是不断查找第K个数,但时间复杂度比较高。O((M+N)*K)

这里使用排列组合思想。时间复杂度O((M+N)*log(M))

首先分析共有多少可能,即排除Impossible。一共M+N位数字,M个‘1‘,则共有种可能,凡是大于此数的K均为不合法。

然后对结果从高位到低位分析,若第1位是‘0‘,剩下的数字中就有M个‘1‘,N-1个0,共有种可能。

  • 若K<k_max,则第1位是‘0‘
  • 若K>k_max,则第1位是‘1‘,K = K - k_max
  • 若K==k_max,则前M位均为‘1‘,剩下的N位为‘0‘

简单思路的程序

排列组合的程序

#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::vector;

int cc(int s, int x)
{
	int result = 0;
	double temp = 1.0;
	for (int i = 0; i < x; i++)
	{
		temp *= (double)(s - i) / (double)(x - i);
	}
	result = (int)(temp + 0.0001);
	return result;
}
int main(int argc, char **argv)
{
	vector<vector<int>> input;
	int total = 0;
	cin >> total;
	while (total--)
	{
		int N, M, K;
		cin >> N >> M >> K;
		vector<int> temp;
		temp.push_back(N);
		temp.push_back(M);
		temp.push_back(K);
		input.push_back(temp);
	}

	for (int i = 0; i < input.size( ); i++)
	{
		int N, M, K;
		N = input[i][0];
		M = input[i][1];
		K = input[i][2];
		//cout << N << ", " << M << ", " << K << endl;

		int max = cc(M + N, N);
		if (max < K)
		{
			cout << "Imposibile" << endl;
		}

		int n, m;
		int _k = K;
		char *digits = new char[M + N + 1];
		int idx = 0;
		memset(digits, 0, M + N + 1);
		m = M; n = N;
		while (idx < M + N)
		{
			// if highest is 0;
			int t = cc(m + n - 1, n - 1);
			if (t > _k)
			{
				digits[idx++] = ‘0‘;
				m = m;
				n = n - 1;
			}
			else if (t < _k)
			{
				digits[idx++] = ‘1‘;
				m = m - 1;
				n = n;
				_k = _k - t;
			}
			else if (t == _k)
			{
				digits[idx++] = ‘0‘;
				n--;
				while (m--)
				{
					digits[idx++] = ‘1‘;
				}
				while (n--)
				{
					digits[idx++] = ‘0‘;
				}
			}
		}
		cout << digits << endl;
		delete[] digits;
	}

}

  

微软2014实习生在线测试之K-th string,布布扣,bubuko.com

时间: 2024-10-07 05:23:02

微软2014实习生在线测试之K-th string的相关文章

【微软2014实习生及秋令营技术类职位在线测试】题目3 : Reduce inversion count

时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description Find a pair in an integer array that swapping them would maximally decrease the inversion count of the array. If such a pair exists, return the new inversion count; otherwise returns the original invers

【微软2014实习生及秋令营技术类职位在线測试】题目2 : K-th string

时间限制:10000ms 单点时限:1000ms 内存限制: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

微软2014实习生及校招秋令营技术类职位,在线编程题目及解答。

题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program is required to process an input string containing only ASCII characters between '0' and '9', or between 'a' and 'z' (including '0', '9', 'a', 'z'). Y

2014阿里巴巴WEB前端实习生在线笔试题

2014年3月31日晚,我怀着略微忐忑的心情(第一次在线笔试^_^!!)进行了笔试,阿里巴巴的笔试题共有10道,几乎包含了Web前端开发的各个方面,有程序题.有叙述题,时间非常紧张,只完成了大概6道题.下面把遇到的题目跟大家分享一下! 1. <pre name="code" class="html"><!doctype html> <html> <head> <style type="text/css&

2015阿里巴巴前端实习生在线笔试题

Summary 大公司开始招实习生了,我也变成过来人了,品味到之前的酸甜苦辣,除了加油好像也没法说那么多. 因为是你在奋斗,心态这件事是你们在掌握的.但是我们唯一能提供的是我们topview实验室新鲜出炉的面经和笔试. (其实我在想有没应届生春招 - -!) Where 2015阿里巴巴前端实习生在线笔试题

微软2014编程之美初赛第一场——题目2 : 树

[来源] 题目2 : 树 [分析] 依据输入情况建立起树的模型.树的表示是一个表明父亲节点的数组.核心算法有两个: 计算某一节点的深度.用循环实现,一直向上找父亲节点,直到找到根节点.计算循环的次数即为深度. 计算某一节点的全部子节点.用递归实现. 本题在实现上节点的命名从0至N-1,与题目描写叙述不同. [代码] #include <iostream> #include <vector> using namespace std; vector<int> childre

微软2014年技术岗位在线笔试题

DescriptionFor this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’). Your program should reorder and split all input string char

2017百度web前端实习生在线笔试题

代码: 1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner sc = new Scanner(System.in); 6 int n = sc.nextInt(); 7 int num[]=new int[n];//用户输入的数组 8 int b[]=new int[n];//复制num 9 int c[]=new int[n];//依次保

2015阿里巴巴前端实习生在线笔试考后总结

写在前面 还是太年轻,第一次在线笔试有些紧张了 一.2015题目 我遇到的题目:6个选择其中3个多选,1个填空,6个大题.客服姐姐说题目是随机给的(因为给了一个时段考试,而不是统一时间点开考),不过题型应该是固定的. 单选:一个数组,两个引用,相互赋值,问输出 眩晕抗性-30% 单选:问一个return匿名函数的函数的执行结果,内部还有apply 眩晕抗性再-69% 单选:问字符串替换结果是什么,当然,又是套了几层,绕了几圈 眩晕抗性再-1%,嗯,做完这道给彻底绕晕了 多选:移动端,如果A按钮上