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

时间:2023-01-14 23:54:21
时间限制: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 order. If such 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”.

例子输入

3
2 2 2
2 2 7
4 7 47

例子输出

0101
Impossible
01010111011

思路:带有反复字符的全排列问题

import java.lang.String;
import java.lang.StringBuilder;
import java.net.Inet4Address;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner; public class Permutation {
static int sum ; public static void main(String[] args)
{
int T ;
Scanner jin = new Scanner(System.in);
T = jin.nextInt();
while(T-- > 0)
{
int N, M, K;
StringBuilder stringBuilder = new StringBuilder();
N = jin.nextInt();M = jin.nextInt();K = jin.nextInt(); if (!((M+N) >= 2 && (N+M) <= 33)) {
System.out.println("Impossible");
continue;
} if(N < 0 || M < 0)
{
System.out.println("Impossible");
continue;
} for (int i = 0; i < N; i++) {
stringBuilder.append('0');
}
for (int i = 0; i < M; i++) {
stringBuilder.append('1');
}
sum = 0;
permutation(stringBuilder.toString().toCharArray(), 0, stringBuilder.length()-1, K);
if (sum < K) {
System.out.println("Impossible");
}
}
}
public static void permutation(char[] str, int start, int end, int K) {
if (start == end) {
sum++;
//System.out.print(sum);
//System.out.print(" ");
//System.out.print(new String(str));
//System.out.print(" ");
if (sum == K) {
System.out.println(new String(str));
return ;
} }
else {
for (int i = start; i <= end; i++) {
if(!Isduplicated(str, start, i))
{
char tmp = str[start];
str[start] = str[i];
str[i] = tmp; permutation(str, start+1, end, K); tmp = str[start];
str[start] = str[i];
str[i] = tmp;
} }
} }
public static boolean Isduplicated(char[] ch, int start, int end) {
for (int i = start; i < end; i++) {
if (ch[i] == ch[end]) {
return true;
}
}
return false;
} }

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

  1. 微软2014实习生及秋令营技术类职位在线测试(题目1 &colon; String reorder)

    题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program ...

  2. 微软2014实习生招聘笔试第2题 the k-th string

    Time Limit: 10000msCase Time Limit: 1000msMemory Limit: 256MB Description Consider a string set that ...

  3. Luogu 2018 秋令营 Test 2

    T1: 题目描述 你正在使用过时的浏览器,洛谷暂不支持. 请 升级浏览器 以获得更好的体验! Bob 来到了一个 $n \times m$ 的网格中,网格里有 $k$ 个豆子,第 $i$ 个豆子位于 ...

  4. 微软2014校招笔试题-String reorder

    Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB Description For this question, your pro ...

  5. 9月5日 华为2014校园招聘的机试题目&lowbar;C语言版答案

    手有些生了. 题目: 通过键盘输入一串小写字母(a~z)组成的字符串.请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串.压缩规则:1.仅压缩连续重复出现的字符.比如 ...

  6. 阿里巴巴2014年校园招聘(秋季招聘)在线笔试--測试研发project师

    第一部分是单选题:40分钟答题时间. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveG1oMTk1NA==/font/5a6L5L2T/fontsize/ ...

  7. 2014年第五届蓝桥杯B组&lpar;C&sol;C&plus;&plus;&rpar;预赛题目及个人答案&lpar;欢迎指正&rpar;

    参考:https://blog.csdn.net/qq_30076791/article/details/50573512 第3题: #include<bits/stdc++.h> usi ...

  8. 2014小米,百度,pptv,去哪儿笔试题目回忆

    今天一共笔试了这四家,真累啊,上午10点小米,下午2点百度,下午3点PPTV,下午5点去哪儿,今天右手太酸了,打的都话了50左右,如果没面试通知,那我可亏大了 小米就三题: 1.一个数组,排序要求,所 ...

  9. Hua Wei 机试题目四---2014

    一.计算亮灯的个数 描述:一条长廊里依次装有n(1≤n≤65535)盏电灯,从头到尾编号1.2.3.…n-1.n.每盏电灯由一个拉线开关控制.开始,电灯全部关着. 有n个学生从长廊穿过.第一个学生把号 ...

随机推荐

  1. mysql数据库 中文乱码

    看到一篇很好的文章,转录于此 中文乱码似乎是程序编写中永恒的一个话题和难点,就比如MySQL存取中文乱码,但我想做任何事情,都要有个思路才行,有了思路才知道如何去解决问题,否则,即使一时解决了问题,但 ...

  2. Undefined symbols for architectureIOS

    IOS问题解决. 现在进行老项目的编译,发现不能编译. 经过各种盲目查询,找个几个方案. 1.builde setting修改编译方式. 2.Builde Phases(修改). 2.1.库. 2.1 ...

  3. 转载RabbitMQ入门(1)--介绍

    目录[-] "Hello World" (使用java客户端) 发送 接收 把所有放在一起 前面声明本文都是RabbitMQ的官方指南翻译过来的,由于本人水平有限难免有翻译不当的地 ...

  4. 通过开发工具发布web应用到tomcat服务器中--对于小白,大神可以忽略不看,勿喷,谢谢

    需要的工具 MyEclipse和TomCat 本人用的是MyEclipse2014和TomCat7 TomCat结构图 第一步:在MyEclipse中配置TomCat 如图所示: 第二步:创建Web项 ...

  5. zepto&period;js介绍(持续更新)

    前言: zepto是一个简化版的jQuery,主要针对移动端开发. 简化了jQuery里很多的浏览器兼容性代码,jQuery的很多方法都被拿掉了(eg:slideUp). WP设备兼容性很差. 官方链 ...

  6. 【Linux学习三】Linux系统目录架构

    主要包括: ●bin:保存的是可执行文件,二进制,就是命令 ●boot:引导目录,操作系统的启动加载,包含版本内核文件.greb引导程序- ●dev:硬件设备文件,如硬盘.网卡.声卡.终端.显卡,每一 ...

  7. c&num;中&commat;标志的作用

    参考微软官方文档-特殊字符@,地址 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/tokens/verbatim ...

  8. javaWeb中MVC的编程思想示例

    没有学习MVC之前我只写了一个Servlet类(Note_List.java),分层之后,我将这个类分成了5个类(NoteDao.java,,NoteDaoImpl.java,,NoteService ...

  9. linux 查看系统磁盘、内存大小

    1.磁盘 df -h cat /proc/partitions 2.内存 cat /proc/meminfo cat /proc/meminfo

  10. &lpar;最小生成树&rpar; Jungle Roads -- POJ -- 1251

    链接: http://poj.org/problem?id=1251 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2177 ...