2014微软实习生招聘第二题
2014-04-12 23:39
169 查看
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 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”.
Sample In
3
2 2 2
2 2 7
4 7 47
Sample Out
0101
Impossible
01010111011
我的细节处理并不高明,但是套路和大家都相似,对第k个字典序string temp,从后往前遇到'1','0'可交换时候,把此前的'1'全部放到最后。直接上代码
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 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”.
Sample In
3
2 2 2
2 2 7
4 7 47
Sample Out
0101
Impossible
01010111011
我的细节处理并不高明,但是套路和大家都相似,对第k个字典序string temp,从后往前遇到'1','0'可交换时候,把此前的'1'全部放到最后。直接上代码
#include "stdafx.h" #include <cstdio> #include <cstdlib> #include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <string> using namespace std; bool permutatiom(string s, int k, string &result,int n,int m) { string temp=s; string out; for(int i=0;i<m;i++) { out.push_back('1'); } for(int i=0;i<n;i++) { out.push_back('0'); } int i=1; for( ;i<k && temp.compare(out)!=0;i++) { int count=0; for(int j=n+m-1;j>0;j--) { //int count=0; if(temp[j]=='1') count++; if(temp[j]=='1' && temp[j-1]=='0') { temp[j]='0'; temp[j-1]='1'; count--; for(int ii=m+n-1;ii>m+n-1 -count;ii--) { temp[ii]='1'; } for(int ii=m+n-1-count;ii>j;ii--) { temp[ii]='0'; } break; } } //cout<<temp<<endl; } if(i==k) { result=temp; return true; } else { //cout<<i<<endl; //cout<<temp<<endl; return false; } } int main() { int ss; int n,m,k; cin>>ss; while(ss--) { cin>>n>>m>>k; if(k<=0) { cout<<"Impossible"<<endl; break; } string s; for(int i=0;i<n;i++) { s.push_back('0'); } for(int i=0;i<m;i++) { s.push_back('1'); } string result; if(permutatiom(s,k,result,n,m)) { cout<<result<<endl; } else cout<<"Impossible"<<endl; } //system("pause"); return 0; }
相关文章推荐
- 微软2014实习生招聘在线测试第2题——K-th string
- 微软2014实习生招聘在线测试第1题——string reorder
- 微软2014实习生招聘笔试第2题 the k-th string
- 金山网络2014春季Android实习生招聘-成都站-笔试第二题
- 2014-04网易、微软、百度、腾讯、阿里实习生招聘经验与经过
- 华为2014暑期实习生招聘机试第二题
- 微软2014实习生招聘编程测试string reorder
- 微软2014实习生在线测试之String reorder .
- 微软2014校园招聘笔试试题
- 微软2014实习生及秋令营技术类职位在线测试+试题1+答案
- 微软2014实习生及秋令营技术类职位在线测试——K-th string
- 腾讯2014实习生招聘一道附加题 DP和分治两种解法
- 【微软2014实习生及秋令营技术类职位在线测试】题目2 : K-th string
- 微软2014实习生及校招秋令营技术类职位,在线编程题目及解答。
- 微软2014实习生及秋令营技术类职位在线测试题目1+题目2
- 2017微软秋季校园招聘在线编程笔试(第二题)
- 微软2012暑期实习生校园招聘笔试题
- 微软2012暑期实习生校园招聘笔试题
- 微软2014实习生及秋令营技术类职位在线测试_题目3 : Reduce inversion count
- 微软2014实习生及秋令营技术类职位在线测试——Reduce inversion count