Collection of programming questions

1. All characters are arranged

Input a string str (may have multiple characters repeated), output the full permutation of the string, and output the result in lexicographical order.

example:

Input:
abc
Output:
abc
acb
bac
bca
cab
cba

Parse:

(1) Method 1: Sort the string str, and then call the next_permutation function in STL to output the full permutation of the string.
(2) Method 2: Find the full arrangement of strings by regularity.
First , find all the characters that may appear in the first position;
secondly , exchange the first character with the following characters one by one;
then , fix the first character character, find the arrangement of all the following characters. At this time, we still divide all the following characters into two parts: the first character of the following character, and all the characters after this character. Then swap the first character with the characters after it one by one.
Since there are repeated characters, the character arrangement is performed as above, and the same arrangement may exist. Therefore, it is necessary to remove the duplicates. There are mainly two methods. One method is to use a set to record whether a certain arrangement exists . A non-recurring number exchange . For example, "abb", the first character a and the second character b are exchanged to get "bab". At this time, since the second character is the same as the third character, the first character a is not exchanged with the third character. Consider "bab" again, the second character differs from the third and is swapped for "bba".

C++ code implementation:
Method 1:

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
using namespace std;

void qpl(string line)
{
    int len = line.size();
    if(len<=1){
        cout<<line;
        return;
    }
    char p[len+1];
    line.copy(p,len,0);
    p[len] = 0;
    sort(p,p+len);
    do{
        cout<<p<<endl;
    }while(next_permutation(p,p+len)); //有序的
}
int main()
{
    string line;
    cin>>line;
    qpl(line);
    return 0;
}

Method 2: Hash table deduplication

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
using namespace std;

unordered_set<string> dict;

void pailie(char*s1, char*s2)
{
    if(!s1 || !s2)
        return;
    char x;
    if(*s2=='\0'){
        string str(s1);
        if(dict.find(str)==dict.end())
            dict.insert(string(s1));
    }
    else{
        for(char*t = s2; *t!='\0'; t++){
             x = *t;
            *t = *s2;
            *s2 = x;

            pailie(s1,s2+1);

            x = *t;
            *t = *s2;
            *s2 = x;
        }
    }
}

void qpl(string line)
{
    int len = line.size();
    if(len<=1){
        cout<<line;
        return;
    }
    char p[len+1];
    line.copy(p,len,0);
    p[len] = 0;
    pailie(p,p);

    vector<string> words(dict.begin(),dict.end());
    sort(words.begin(),words.end());
    for(string s:words)
        cout<<s<<endl;
}

int main()
{
    string line;
    cin>>line;
    qpl(line);
    return 0;
}

Determine if a tree is a BST

Input a tree to determine whether the number is a BST

Parse:

Every time we recurse downward, we only need to take the value of the node as a maximum value and pass it to its left node, that is, the value of all nodes on the left is smaller than it; and take its value as the minimum value, The value passed to its right node, that is, all nodes on the right must be greater than it. Each time you go down, update the maximum and minimum values ​​respectively.

C++ code implementation:

bool isBSTHelper(Node p, int low, int high) {  
  if (p == null) return true;  
  if (low < p.data && p.data < high)  
    return isBSTHelper(p.leftChild, low, p.data) &&  
           isBSTHelper(p.rightChild, p.data, high);  
  else  
    return false;  
}  

bool isBST(Node root) {  
  return isBSTHelper(root, INT_MIN, INT_MAX);  
}  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326267906&siteId=291194637