简体   繁体   English

c++ 中排列的回溯

[英]Backtracking for permutations in c++

I currently study permutations of a string where backtracking is used, and I am stuck at figuring out why does does this code goes back to i=0 when the function says it is on 1 for example, and so forth.我目前正在研究使用回溯的字符串的排列,并且我一直在弄清楚为什么当 function 说它在 1 时,为什么这段代码会回到 i=0,等等。 But I do not understand a lot of this code so if anyone wants to help to explain this code to me I would be super thankful, thanks in advance!但我不明白很多这段代码,所以如果有人想帮助向我解释这段代码,我会非常感激,提前谢谢!

#include <iostream>
using namespace std;

// Function to find all Permutations of a given string str[i..n-1]
// containing all distinct characters
void permutations(string str, int i, int n)
{
    // base condition
    if (i == n - 1)
    {
        cout << str << endl;
        return;
    }

    // process each character of the remaining string
    for (int j = i; j < n; j++)
    {
        // swap character at index i with current character
        swap(str[i], str[j]);        // STL swap() used

        // recur for string [i+1, n-1]
        permutations(str, i + 1, n);

        // backtrack (restore the string to its original state)
        swap(str[i], str[j]);
    }
}

// Find all Permutations of a string
int main()
{
    string str = "ABC";

    permutations(str, 0, str.length());

    return 0;
}

I'll go through the steps it's taking to print all the permutations of the string "ABC".我将通过打印字符串“ABC”的所有排列所采取的步骤来 go。

1 - You call the function with i = 0 . 1 - 您使用i = 0调用 function 。

2 - You enter the for loop and start with j = i . 2 - 您进入 for 循环并从j = i开始。

3 - You swap str[0] with str[0] . 3 - 你用 str[0] 交换str[0] str[0] (That does nothing). (什么都不做)。

4 - You recurse with i = i + 1 (i = 1). 4 - 你用i = i + 1 (i = 1) 递归。

5 - You enter the loop again but this time j = i = 1 . 5 - 您再次进入循环,但这次j = i = 1

6 - You swap str[1] with str[1] (That does nothing). 6 - 您将str[1]str[1]交换(什么都不做)。

7 - You do recurse with i = 2 and str = "ABC" . 7 - 你用i = 2str = "ABC"进行递归。 Since i == n - 1 is true, this time you'll just print the result.由于i == n - 1为真,这一次您只需打印结果。

8 - You return from the call to the loop (with i = 1 ). 8 - 您从调用返回到循环(使用i = 1 )。

9 - You undo the swap that you've done before. 9 - 您撤消之前所做的交换。 Note that swapping the 2 elements twice is like doing nothing.请注意,两次交换 2 个元素就像什么都不做。 In the first swap, the elements are swapped.在第一次交换中,元素被交换。 In the second swap, the elements are back as they were before.在第二次交换中,元素又回到了原来的样子。 In this case where i = j , You've swapped the same element with it self, so there is no effect form undoing what you did.在这种情况下i = j ,您已经将相同的元素与它自己交换,因此没有效果形式撤消您所做的事情。 But it'll be required when you swap 2 different characters.但是当你交换 2 个不同的字符时,它是必需的。

10 - j gets incremented and becomes 2, and you swap str[1] with str[2] . 10 - j增加并变为 2,然后将str[1]str[2]交换。

11 - You recurse again and call the function with i = 2 and str = "ACB" . 11 - 您再次递归并使用i = 2str = "ACB"调用 function 。 Again, this time you'll just print the result.同样,这一次您只需打印结果。

I think you can deduce what will happen next and how the rest of the strings are formed.我想你可以推断出接下来会发生什么,以及琴弦的 rest 是如何形成的。

If you wanna follow the whole steps you can read this:如果您想遵循整个步骤,可以阅读以下内容:

Entered the function with i = 0 and str = ABC
    Swapping str[0] (A) with str[0] (A)
    str = ABC
    Entered the function with i = 1 and str = ABC
        Swapping str[1] (B) with str[1] (B)
        str = ABC
        Entered the function with i = 2 and str = ABC
            Printing the string ABC
        Swapping str[1] (B) with str[1] (B) to restore the original state of the string
        str = ABC
        Swapping str[1] (B) with str[2] (C)
        str = ABC
        Entered the function with i = 2 and str = ACB
            Printing the string ACB
        Swapping str[1] (C) with str[2] (B) to restore the original state of the string
        str = ABC
    Exiting the function with i = 1 and str = ABC
    Swapping str[0] (A) with str[0] (A) to restore the original state of the string
    str = ABC
    Swapping str[0] (A) with str[1] (B)
    str = ABC
    Entered the function with i = 1 and str = BAC
        Swapping str[1] (A) with str[1] (A)
        str = BAC
        Entered the function with i = 2 and str = BAC
            Printing the string BAC
        Swapping str[1] (A) with str[1] (A) to restore the original state of the string
        str = BAC
        Swapping str[1] (A) with str[2] (C)
        str = BAC
        Entered the function with i = 2 and str = BCA
            Printing the string BCA
        Swapping str[1] (C) with str[2] (A) to restore the original state of the string
        str = BAC
    Exiting the function with i = 1 and str = BAC
    Swapping str[0] (B) with str[1] (A) to restore the original state of the string
    str = ABC
    Swapping str[0] (A) with str[2] (C)
    str = ABC
    Entered the function with i = 1 and str = CBA
        Swapping str[1] (B) with str[1] (B)
        str = CBA
        Entered the function with i = 2 and str = CBA
            Printing the string CBA
        Swapping str[1] (B) with str[1] (B) to restore the original state of the string
        str = CBA
        Swapping str[1] (B) with str[2] (A)
        str = CBA
        Entered the function with i = 2 and str = CAB
            Printing the string CAB
        Swapping str[1] (A) with str[2] (B) to restore the original state of the string
        str = CBA
    Exiting the function with i = 1 and str = CBA
    Swapping str[0] (C) with str[2] (A) to restore the original state of the string
    str = ABC
Exiting the function with i = 0 and str = ABC

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM