Poker sequence
AA 2 2 3 3 4 4, a total of 4 pairs of playing cards. Please line them up.
Requirements: There is 1 card between the two Aces, 2 cards between the two 2s, 3 cards between the two 3s, and 4 cards between the two 4s.Please fill in the one with the smallest lexicographical order among all the permutations that meet the requirements.
For example: 22AA3344 is lexicographically smaller than A2A23344. Of course, none of them is the answer to the requirements.
Please submit your answers via your browser. "A" must not use lowercase a, and do not use "1" instead. Do not leave spaces between characters.
Answer: 2342A3A4
Problem analysis
Lexicographic order
Baidu Encyclopedia: Dictionary or dictionary order (also called vocabulary order, dictionary order, alphabetical order or dictionary order) is a method of arranging words in alphabetical order based on alphabetical order. This generalization mainly consists in defining the total order of the sequence (often called words in computer science) of the elements of an ordered fully ordered set (often called an alphabet).
For the arrangement of numbers 1, 2, 3...n, the sequence of different arrangements is determined by comparing the corresponding numbers one by one from left to right. For example, for the 5-digit arrangement 12354 and 12345, the arrangement 12345 is in the front and the arrangement 12354 is in the back. According to this rule, the first one in all the arrangements of five numbers is 12345, and the last one is 54321.
Full arrangement
next_permutation(), header file algorithm
Judge whether the condition is met by looking at the subscript difference of the string
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool check(string s){
if(s.rfind('A') - s.find('A') == 2 &&
s.rfind('2') - s.find('2') == 3 &&
s.rfind('3') - s.find('3') == 4 &&
s.rfind('4') - s.find('4') == 5
){
return true;
}
return false;
}
int main(int argc, char** argv) {
string s = "223344AA";
do{
if(check(s)){
cout << s << endl;
}
} while(next_permutation(s.begin(), s.end()));
return 0;
}