OnePointThreeAcres Google/April -1

本文介绍了一种用于生成包含'0'、'1'和'?'的字符串所有可能组合的算法。通过深度优先搜索(DFS)策略,递归地替换字符串中的'?'字符为'0'或'1',最终输出所有可能的组合。

http://www.1point3acres.com/bbs/thread-184034-1-1.html

First Question:

A string consists of ‘0’, ‘1’ and '?'. The question mark can be either '0' or '1'. Find all possible combinations for a string.

01?0  output-->0100, 0110


First question This is very simple DFS:

#include <string>
#include <vector>
#include <iostream>
using namespace std;


void allCombinations(string str, int pos, vector<string>& res, string target) {
    if(target.size() == str.size()) {
        res.push_back(target);
        return;
    }
    for(int i = pos; i < str.size(); ++i) {
        if(str[i] == '?') {
            allCombinations(str, i+1, res, target + '0');
            allCombinations(str, i+1, res, target + '1');
        } else {
            allCombinations(str, i + 1, res, target + str[i]);
        }
    }
}

vector< string > allCombinations(string str) {
    bool exist = false;
    vector<string> res;
    for(int i = 0; i < str.size(); ++i) {
        if(str[i] == '?') {exist = true; break;}
    }
    if(exist == false) {res.push_back(str); return res; }
    else {
        allCombinations(str, 0, res, "");
        return res;
    }
}


// a string only contains 0 and 1 and ?
// substitute the ? with either 0 or 1 and return all possiblities.
// for example:
// 010?1  -> return 01001, 01011

int main(void) {
    vector<string> res = allCombinations("1?0?");
    for(int i = 0; i < res.size(); ++i) {
        cout << res[i] << endl;
 }                                                                                                                                                                         1,1           Top


In this post, there is a very interesting posted. See this piece of code: This will cause repeating because of the bold line. I was first confused about where is the mistake. It is better to draw a graph to show the mistake.


        public ArrayList<String> findCombination(String input) {
                ArrayList<String> res = new ArrayList<String>();
                helper(res, 0, "", input);
                return res;
        }
       
        private void helper(ArrayList<String> res, int step, String cur, String input) {
                if(cur.length() == input.length()) {
                        res.add(new String(cur));
                        return;
                }
                for(int i = step; i < input.length(); i++) {
                        if(input.charAt(i) != '?') {
                                <strong>cur += input.charAt(i);</strong>
                                helper(res, i+1, cur, input);
                        } else {
                                helper(res, i+1, cur + "0", input); 
                                helper(res, i+1, cur + "1", input);
                        }
                }
        }


A more elegant way to solve this confusion:

void allCombinations(string str, int pos, vector<string>& res, string target) {
    if(target.size() == str.size()) {
        res.push_back(target);
        return;
    }
    for(int i = pos; i < str.size(); ++i) {
        if(str[i] == '?') {
            allCombinations(str, i+1, res, target + '0');
            allCombinations(str, i+1, res, target + '1');
        } else {
            target = target + str[i];    // if it is not ?, the situation is confirmed.  It doesn't need to go recursion.
        }
    }
}




ros1@Melodic:~$ cd librealsense-2.51.1 ros1@Melodic:~/librealsense-2.51.1$ mkdir build ros1@Melodic:~/librealsense-2.51.1$ cd build ros1@Melodic:~/librealsense-2.51.1/build$ cmake .. -- The CXX compiler identification is GNU 7.5.0 -- The C compiler identification is GNU 7.5.0 -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Checking internet connection... -- Failed to identify Internet connection CMake Warning at CMakeLists.txt:19 (message): No internet connection, disabling BUILD_WITH_TM2 CMake Warning at CMakeLists.txt:25 (message): No internet connection, disabling IMPORT_DEPTH_CAM_FW -- Info: REALSENSE_VERSION_STRING=2.51.1 -- Performing Test COMPILER_SUPPORTS_CXX11 -- Performing Test COMPILER_SUPPORTS_CXX11 - Success -- Performing Test COMPILER_SUPPORTS_CXX0X -- Performing Test COMPILER_SUPPORTS_CXX0X - Success -- Setting Unix configurations -- Building libcurl enabled -- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.so (found version "1.1.1") -- using RS2_USE_V4L2_BACKEND -- Found usb: /usr/lib/x86_64-linux-gnu/libusb-1.0.so -- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") -- Checking for module 'libudev' -- Found libudev, version 237 -- Found Udev: /usr/include -- GLFW 3.3 not found; using internal version -- Looking for pthread.h -- Looking for pthread.h - found -- Looking for pthread_create -- Looking for pthread_create - found -- Found Threads: TRUE -- Found Vulkan: /usr/lib/x86_64-linux-gnu/libvulkan.so -- Using X11 for window creation -- Looking for XOpenDisplay in /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so -- Looking for XOpenDisplay in /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so - found -- Looking for gethostbyname -- Looking for gethostbyname - found -- Looking for connect -- Looking for connect - found -- Looking for remove -- Looking for remove - found -- Looking for shmat -- Looking for shmat - found -- Looking for IceConnectionNumber in ICE -- Looking for IceConnectionNumber in ICE - found -- Found X11: /usr/lib/x86_64-linux-gnu/libX11.so -- Found OpenGL: /usr/lib/x86_64-linux-gnu/libOpenGL.so -- Could NOT find apriltag (missing: APRILTAG_INC APRILTAG_LIB) -- Unable to find apriltag library, skipping pose-apriltag example -- Check for updates capability added to realsense-viewer -- Check for updates capability added to realsense-depth-quality -- Configuring done -- Generating done -- Build files have been written to: /home/ros1/librealsense-2.51.1/build ros1@Melodic:~/librealsense-2.51.1/build$
最新发布
09-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值