ms-onlinetest-question02

本文介绍了一种算法,用于在由相同数量的0和1组成的二进制字符串集合中,按字典序查找第K个字符串。通过递归构造二进制序列,解决了输入字符串长度限制在33内且0、1数量相等的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question 2

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

代码:(不确定是否AC)

#pragma once 
/*
@note: 这个题目模拟生成升序二进制序列,有点意思 :)
@algm: 如果采用构造这样的二进制序列的话,可看做递归式的"1"与"0"的移动
@date: 04/14/2014
@author: worksdata@163.com
*/

#include <iostream>

using namespace std;

#define  SAFE_DEL_POINTER(p) if(p) {delete[] p; p = NULL;} 

void FindBinarySequence(int nZero, int nOne, int kth);
void NextSequence(char*& szBiSeq, int nPosFirstOne, int nLen, int& nID, int kth);
void SwapChar(char& a, char& b);

void RunQuest02()
{
    int nCount = 0;
    cin>>nCount;
    if(nCount > 0)
    {
        int* a = new int[nCount];
        memset(a, 0, nCount);
        int* b = new int[nCount];
        memset(b, 0, nCount);
        int* c = new int[nCount];
        memset(c, 0, nCount);

        cout<<endl;//"input the cases:"
        for(int i = 0; i < nCount; ++i)
            cin>>a[i]>>b[i]>>c[i];

        cout<<endl;//<<"finding.."
        for(int i = 0; i < nCount; ++i)
        {
            FindBinarySequence(a[i], b[i], c[i]);
        }

        SAFE_DEL_POINTER(a);
        SAFE_DEL_POINTER(b);
        SAFE_DEL_POINTER(c);
    }    

}

void FindBinarySequence(int nZero, int nOne, int kth)
{
    if(nZero<0 || nOne < 0 || nZero+nOne<2 || nZero+nOne > 33 || kth < 1 ||  kth > 1000000000)
    {
        cout<<"impossible"<<endl;
        return;
    }

    char* szBiSeq = new char[nZero+nOne+1];
    for(int i = 0; i < nZero+nOne; ++i)
    {
        if(i < nZero)
            szBiSeq[i] = '0';
        else
            szBiSeq[i] = '1';
    }
    szBiSeq[nZero+nOne] = '\0';

    //output the first binary sequence
    int nID = 1;
    //cout<<szBiSeq<<"\t"<<nID<<endl;
    NextSequence(szBiSeq, nZero, nZero+nOne, nID, kth);
    if(nID < kth)
        cout<<"impossible"<<endl;

    if(szBiSeq)
    {
        delete szBiSeq;
        szBiSeq = NULL;
    }
}

void NextSequence(char*& szBiSeq, int nPosFirstOne, int nLen, int& nID, int kth)
{
    //"1" 与最左边的"0"交换位置,同时所有的"1"靠向最右边
    if(nPosFirstOne == 0 || nID == kth)
    {
        cout<<szBiSeq<<endl;
        return;
    }

    SwapChar(szBiSeq[nPosFirstOne-1], szBiSeq[nPosFirstOne]);
    int nOneCount = 0;
    for(int i = nPosFirstOne+1; i < nLen; ++i)
    {
        if(szBiSeq[i] == '1')
        {
            ++nOneCount;
            szBiSeq[i] = '0';
        }
    }

    int i = nLen-1;
    int n = nOneCount;
    for(; i > nPosFirstOne && n > 0; --i, --n)
        szBiSeq[i] = '1';
    //new binary sequence
    //cout<<szBiSeq<<"\t"<<++nID<<endl;
    ++nID;

    if(nOneCount > 0)
        NextSequence(szBiSeq, i+1, nLen, nID, kth);
    else        
    {
        //从右边开始找到"1"出现后的第一个"0"
        bool bOneOccur = false;
        bool bFound = false;
        int nFirstOne = 0;
        
        for(int i = nLen -1; i >= 0; --i)
        {
            if(szBiSeq[i] == '1')
                bOneOccur = true;
            else if(szBiSeq[i] == '0' && bOneOccur)
            {
                bFound = true;
                nFirstOne = i + 1;
                break;
            }
        }
        if(bFound)
            NextSequence(szBiSeq, nFirstOne, nLen, nID, kth);
    }
}

void SwapChar(char& a, char& b)
{
    a = a + b;
    b = a - b;
    a = a - b;
}

 

转载于:https://www.cnblogs.com/tupx/p/3663901.html

为了在Windows安装ADB工具,你可以按照以下步骤进行操作: 1. 首先,下载ADB工具包并解压缩到你自定义的安装目录。你可以选择将其解压缩到任何你喜欢的位置。 2. 打开运行窗口,可以通过按下Win+R键来快速打开。在运行窗口中输入"sysdm.cpl"并按下回车键。 3. 在系统属性窗口中,选择"高级"选项卡,然后点击"环境变量"按钮。 4. 在环境变量窗口中,选择"系统变量"部分,并找到名为"Path"的变量。点击"编辑"按钮。 5. 在编辑环境变量窗口中,点击"新建"按钮,并将ADB工具的安装路径添加到新建的路径中。确保路径正确无误后,点击"确定"按钮。 6. 返回到桌面,打开命令提示符窗口。你可以通过按下Win+R键,然后输入"cmd"并按下回车键来快速打开命令提示符窗口。 7. 在命令提示符窗口中,输入"adb version"命令来验证ADB工具是否成功安装。如果显示版本信息,则表示安装成功。 这样,你就成功在Windows安装ADB工具。你可以使用ADB工具来执行各种操作,如枚举设备、进入/退出ADB终端、文件传输、运行命令、查看系统日志等。具体的操作方法可以参考ADB工具的官方文档或其他相关教程。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [windows环境安装adb驱动](https://blog.youkuaiyun.com/zx54633089/article/details/128533343)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Windows安装使用ADB简单易懂教程](https://blog.youkuaiyun.com/m0_37777700/article/details/129836351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值