简体   繁体   English

查找可以由保留字符串顺序的第一和第二个字符串的所有字符组成的给定字符串的所有交织

[英]Find all interleavings of given strings that can be formed from all the characters of first and second string where order of characters is preserved

I am trying to find all interleavings of given strings that can be formed from all the characters of first and second string where order of characters is preserved 我试图找到可以由保留字符串顺序的第一和第二字符串的所有字符组成的给定字符串的所有交织

I tried to use recursion. 我试图使用递归。

// This code works[Snippet 1]
void func(string str1,string str2,string temp){

    if(!str1.length() && !str2.length()){
        cout << temp << endl;
        return;
    }

    if(str1.length()){
        func(str1.substr(1),str2,temp+str1[0]);
    }

    if(str2.length()){
        func(str1,str2.substr(1),temp+str2[0]);
    }
}

// This code does not work[Snippet 2]
void func(string str1,string str2,string temp){

    if(!str1.length() && !str2.length()){
        cout << temp << endl;
        return;
    }

    if(str1.length()){
        temp+=str1[0];
        func(str1.substr(1),str2,temp);
    }

    if(str2.length()){
        temp+=str2[0];
        func(str1,str2.substr(1),temp);
    }
}

The difference between snippet 1 and snippet 2 is that in snippet 2, I have appended the temp string with the character before passing it into the function. 片段1和片段2之间的区别在于,在片段2中,我在将temp字符串附加到该字符之前,将其传递给函数。 The second code gives result(string) whose size is greater than sum of length of both input strings. 第二个代码给出result(string),其大小大于两个输入字符串的长度之和。

Here's a version with plenty of cout and a way to track recursion depth. 下面是用大量的版本cout和方法来跟踪递归深度。

#include <iostream>
#include <string>

using namespace std;

void func0(string str1, string str2, string temp, int depth) {

    if (!str1.length() && !str2.length()) {
        cout << temp << endl;
        depth--;
        return;
    }

    if (str1.length()) {
        cout << "depth: " << depth++ << "|temp: " << temp << '\n';
        func0(str1.substr(1), str2, temp + str1[0], depth);
    }

    if (str2.length()) {
        cout << "depth: " << depth++ << "|temp: " << temp << '\n';
        func0(str1, str2.substr(1), temp + str2[0], depth);
    }

    depth--;
}

void func1(string str1, string str2, string temp, int depth) {
    if (!str1.length() && !str2.length()) {
        cout << temp << endl;
        depth--;
        return;
    }

    if (str1.length()) {
        temp += str1[0]; //temp has
        cout << "depth: " << depth++ << "|temp: " << temp << '\n';
        func1(str1.substr(1), str2, temp, depth);
    }

    if (str2.length()) {
        temp += str2[0];
        cout << "depth: " << depth++ << "|temp: " << temp << '\n';
        func1(str1, str2.substr(1), temp, depth);
    }

    depth--;
}

int main(int argc, char* argv[]) {
    string a = "asd";
    string b = "qw";
    string c = "";

    cout << "func0\n";
    func0(a, b, c, 0);
    cout << "func1\n";
    func1(a, b, c, 0);

    return 0;
}

which yields this output for func0: 这将为func0产生以下输出:

func0
depth: 0|temp:
depth: 1|temp: a
depth: 2|temp: as
depth: 3|temp: asd
depth: 4|temp: asdq
asdqw
depth: 3|temp: as
depth: 4|temp: asq
depth: 5|temp: asqd
asqdw
depth: 5|temp: asq
depth: 6|temp: asqw
asqwd
depth: 2|temp: a
depth: 3|temp: aq
depth: 4|temp: aqs
depth: 5|temp: aqsd
aqsdw
depth: 5|temp: aqs
depth: 6|temp: aqsw
aqswd
depth: 4|temp: aq
depth: 5|temp: aqw
depth: 6|temp: aqws
aqwsd
depth: 1|temp:
depth: 2|temp: q
depth: 3|temp: qa
depth: 4|temp: qas
depth: 5|temp: qasd
qasdw
depth: 5|temp: qas
depth: 6|temp: qasw
qaswd
depth: 4|temp: qa
depth: 5|temp: qaw
depth: 6|temp: qaws
qawsd
depth: 3|temp: q
depth: 4|temp: qw
depth: 5|temp: qwa
depth: 6|temp: qwas
qwasd

and this for func1: 而对于func1:

func1
depth: 0|temp: a
depth: 1|temp: as
depth: 2|temp: asd
depth: 3|temp: asdq
depth: 4|temp: asdqw
asdqw
depth: 3|temp: asdq
depth: 4|temp: asdqd
depth: 5|temp: asdqdw
asdqdw
depth: 5|temp: asdqdw
depth: 6|temp: asdqdwd
asdqdwd
depth: 2|temp: asq
depth: 3|temp: asqs
depth: 4|temp: asqsd
depth: 5|temp: asqsdw
asqsdw
depth: 5|temp: asqsdw
depth: 6|temp: asqsdwd
asqsdwd
depth: 4|temp: asqsw
depth: 5|temp: asqsws
depth: 6|temp: asqswsd
asqswsd
depth: 1|temp: aq
depth: 2|temp: aqa
depth: 3|temp: aqas
depth: 4|temp: aqasd
depth: 5|temp: aqasdw
aqasdw
depth: 5|temp: aqasdw
depth: 6|temp: aqasdwd
aqasdwd
depth: 4|temp: aqasw
depth: 5|temp: aqasws
depth: 6|temp: aqaswsd
aqaswsd
depth: 3|temp: aqaw
depth: 4|temp: aqawa
depth: 5|temp: aqawas
depth: 6|temp: aqawasd
aqawasd

To help see the recursion look at the correct output when depth is 1 as the first level of a tree with 2 branches. 为了帮助您查看递归,请在深度为1时将正确的输出视为具有2个分支的树的第一级。 They should be just the first characters of each string. 它们应该只是每个字符串的第一个字符。 Now each of those nodes get children for each remaining character. 现在,这些节点中的每个节点都会为每个剩余字符获得子级。 At the next lower level (depth 2), you add a character then each of those nodes gets children to create level/depth 3...etc. 在下一个较低的级别(深度2),您添加一个字符,然后每个节点都将使子节点创建级别/深度3 ...等。 It builds a trie when done correctly and temp only changes length once at each depth. 正确完成后,它会建立一个特例,并且temp在每个深度仅更改一次长度。

In both, you bottom out and print when you reach a leaf. 在这两种方法中,您都将触底并在到达叶子时进行打印。

In the incorrect version, you'll see that length of temp is inconsistent in a given depth of the recursion. 在不正确的版本中,您将看到在给定的递归深度中temp长度不一致。 As already said but in a different way, appending to temp and storing it locally (which can also be thought of as appending while still in a row/level/depth of the trie) causes length of temp to grow both left to right and top to bottom. 如前所述,但以不同的方式,将temp附加并存储在本地(也可以将其视为仍在特里的行/级别/深度中附加)会导致temp长度从左到右和从顶部增加到底部。

I think you should use std::next_permutation(). 我认为您应该使用std :: next_permutation()。 For the first string, add for each character a 0 to a vector. 对于第一个字符串,为每个字符向向量添加0。 Do the same for the second string with a 1. Now you have one vector to permutate. 使用1对第二个字符串执行相同的操作。现在,您可以对一个向量进行排列。 For each permutation, simply remap the indices back to the next character in first string if it is even and second when odd. 对于每个排列,只需将索引重新映射回第一个字符串中的下一个字符(如果它为偶数),第二个就为奇数。

Example permutation : "cat", "bird" to (0 0 0 1 1 1 1), one permutation becomes (0 1 1 0 1 0 1 ) which you can remap to "cbiartd" 排列示例:“ cat”,“ bird”到(0 0 0 1 1 1 1 1),一个排列变成(0 1 1 0 1 0 1),您可以将其重新映射到“ cbiartd”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找给定字符串中的所有非重复字符 - Finding ALL Non Repeating characters in a given string 从字符串中删除所有字符 - remove all characters from a string 通过从字符串中选择字符可以形成多少个回文? - How many palindromes can be formed by selections of characters from a string? 通过重新排列字符来查找字符串中的所有回文子字符串 - Find All Palindrome Substrings in a String by Rearranging Characters 检查字符串中的所有字符是否为1、0或“” - Check if all characters in a string are 1, 0, or “ ” 给定一组字符,如何仅通过重新排列字符来显示具有更高字典顺序的字符串? - Given a set of characters, how can you display the string with a higher lexicographical order, only by rearranging the characters? 编写一个简短的C ++程序,输出使用每个字符“ a”,“ b”,“ c”,“ d”,“ e”和“ f”恰好形成一次的所有可能的字符串 - Write a short C++ program that outputs all possible strings formed by using each of the characters ’a’, ’b’, ’c’, ’d’, ’e’, and ’f’ exactly once 首先打印给定序列中的所有数字,然后使用 Array 打印所有字符和其他符号 - first print all numbers in given sequence, then all characters and other symbols using Array 如何使用特定符号查找和替换字符串中的所有字符C ++ - How to find and replace all characters in a string with specific symbols C++ 如何以字符串的不同长度为单位找到所有不同的组合 - How to find all the different combinations as units of different lengths of the characters of a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM