K-th string
这两天参加了hihocoder上的小竞赛,下面把自己做的记录一下!(最痛心的是,开始竟然把main函数,写成了mian,浪费了将近一个小时时间,伤不起啊)
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
这里给出我的答案
思路: 计算由N个0和M个1组成的数的最小值和最大值,然后判断从最小值开始,计算每个数包含一个的个数,到第K的时候,再将其转为包含N个0,和M个1的字符串,输出。(这是暂时想出的方法,以后会仔细思考一下,如果你有不同的方法,欢迎交流)
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
using namespace std; int fac(int n)
{
if(n == )
return ;
else
return n*fac(n-);
}
int numOne(int value)
{
int res = ;
while(value)
{
if(value % == )
res++;
value = value /;
}
return res;
}
string outObj(int value,int len)
{
string res = "";
while(value)
{
if(value % == )
res.append("");
else
res.append("");
value = value / ;
}
reverse(res.begin(),res.end());
string app="";
for(int i=; i < len - res.size(); i++)
app.append("");
app.append(res);
return app;
}
int main()
{
int T;
cin>>T;
int M,N,K;
vector<int> result(T);
int minValue,maxValue;
for(int i=; i<T; i++)
{
cin>>N>>M>>K;
int len = M + N;
minValue=;
maxValue=;
if(fac(N+M)/(fac(N) * fac(M)) < K)
{
cout<<"Impossible"<<endl;
continue;
}
for(int j=; j<M; j++)
{
minValue = minValue + pow(, j);
}
for(int j=N; j<M+N; j++)
{
maxValue = maxValue + pow(, j);
}
int count = ;
int temp = ;
int obj = ;
for(int j = minValue; j <= maxValue; j++)
{
temp = numOne(j);
if(temp == M)
count++;
if(count == K)
{
obj = j;
break;
}
}
string res = outObj(obj,len);
cout<<res<<endl;
}
return ;
}
K-th string的更多相关文章
- [Swift]LeetCode358. 按距离为k隔离重排字符串 $ Rearrange String k Distance Apart
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- LC 358. Rearrange String k Distance Apart
Given a non-empty string s and an integer k, rearrange the string such that the same characters are ...
- 剑指Offer面试题:27.最小的k个数
一.题目:最小的k个数 题目:输入n个整数,找出其中最小的k个数.例如输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 这道题是典型的TopK问题,其最简单的思路莫过于 ...
- golang中string以及slice之间的一些问题
好记性不如烂笔头o_O slice切片不会开辟新的空间 a := []int{0,1,2,3} b := make([]int, 8) b = a[:] b[2] = 9 fmt.Println(a) ...
- Symbols of String Pattern Matching
Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...
- String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)
本章主要介绍String和CharSequence的区别,以及它们的API详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string01. ...
- 关于string的练习题目
/*Are they equal*/#include<iostream>#include<string>using namespace std;int n;string dea ...
- ruby 学习 -- string --1
# define french_string = "il \xc3\xa9tait une fois" long_string = <<EOF Here is a lo ...
- java基础知识回顾之---java String final类普通方法
辞职了,最近一段时间在找工作,把在大二的时候学习java基础知识回顾下,拿出来跟大家分享,如果有问题,欢迎大家的指正. /* * 按照面向对象的思想对字符串进行功能分类. * ...
- 两个string数组对应比较
最近做的array string类型对比.这个可能比较复杂,用的是linq 是请教别人的,我在这里记录一下 jquery 方法里面的数组 function arrtxt() { var arrt= [ ...
随机推荐
- Android实现中文汉字笔划(笔画)、中文拼音排序、英文排序
发布时间:2018-11-16 技术:Android 概述 最近要做一个类似微信的,在登录界面选择国家地区的功能,微信有中文汉字笔画排序以及中文拼音排序等几种方式,如下所示: 简体中文 拼音排 ...
- 子类化QTreeWidgetItem实现增加Item的属性
因为有需求是点击QTreeWidgetItem需要获取该Item的节点的相关属性,Item需要保存关联的属性,那么就需要扩展QTreeWidgetItem,当然,C++中扩展修改一个类或组件的方式就是 ...
- 【laravel5.*】详解laravel中的依赖注入
1.下面这个是自定义的类,钉钉扫码登录web 网页授权OAuth2.0,是一个典型的依赖注入参考示例:
- iOS转让app-您必须移除要转让的 App 的所有构建版本和测试员,并清除“测试信息”下的所有信息字段解决方案
原文详细步骤篇: iOS App转让流程详情教程篇 此文为遇到的一个问题,及如何解决: 问题描述: 转让app遇到这个错误,如何解决? 不解决这个,app是无法进行转让的. 原因分析: 这个是由于Te ...
- java struts2入门学习---文件下载的二种方式
一.关于文件下载: 文件下载的核心思想即是将文件从一个地方拷贝到另一个地方. 1.传统方式: 在Action中加入大量servlet api 操作.优点是好理解,缺点是耦合度高. 2.stream方式 ...
- 开始 App前 需要考虑的几项
来源:Limboy's HQ http://t.cn/R5sEDMJ 随着工具链的完善,语言的升级以及各种优质教程的涌现,做一个 App 的成本也越来越低了.尽管如此,有些事情最好前期就做起来,避免当 ...
- lsof命令详解(转)
lsof命令详解(转) 上一篇 / 下一篇 2011-06-09 21:56:41 / 个人分类:Linux 查看( 351 ) / 评论( 0 ) / 评分( 0 / 0 ) 在Linux中,ls ...
- DPDK的安装与绑定网卡(转)
from:http://www.cnblogs.com/mylinuxer/p/4274178.html DPDK的安装与绑定网卡 DPDK的安装有两种方法: 第一种是使用dpdk/tools/set ...
- SQL如何获得本季度第一天、一年的第一天、本月的最后一天
nterval 参数,具有以下设定值: 设置 描述 Year yy, yyyy 年 quarter qq, q 季 Month mm, m 月 dayofyear dy, y 一年的日数 Day dd ...
- spring-data-redis读写分离
在对Redis进行性能优化时,一直想对Redis进行读写分离.但由于项目底层采用spring-data-redis对redis进行操作,参考spring官网却发现spring-data-redis目前 ...