current position:Home>Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)

Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)

2022-01-27 07:49:03 muse_ age

1. Each element does not repeat

abc,acb,.......

use next_permutataion() Perfect permutation

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    string s;
    cin>>s;
    sort(s.begin(),s.end());
    do
    {
     cout<<s<<endl;
    }while(next_permutation(s.begin(),s.end()));

}

Find the complete arrangement by backtracking ( The arrangement elements are repeatable )

aaa,aab,aac,........

Ideas : Equivalent to the n In the same set , Select an element arrangement for each set

//n Which set of records are selected 
//str Save the results of the previous level of recursion 
void dfs(string s,int n,string str)
{
   int size=s.size();
 // If you choose No size layer , Description: select end , Output str, Empty str( Save a space )
   if(n==size)
  {
     cout<<str;
     str.clear();
     return;
  }
  else
// Do the first n A selection of sets 
 {
    for(int i=0;i<s.size();i++)
   {
     str.push_back(s[i]);// To make a choice 
     dfs(s,n+1,str);// Choose from the next level 
     str.pop_back();
   }
  }

}

copyright notice
author[muse_ age],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270748581432.html

Random recommended