python 改变词典顺序_按词典顺序排列的功率集
python 改變詞典順序
Description:
描述:
This is a standard interview problem to find out the power sets in lexicographic order of a given set of numbers using backtracking.
這是一個標準的面試問題,它使用回溯來按給定數字集的字典順序查找能力集。
Problem statement:
問題陳述:
There is a set contains N no. of unsorted characters. You have to find out the power sets in lexicographic order of a given set of numbers and print them.
有一組包含N個編號。 未排序的字符。 您必須按字典順序找出給定數字集的冪集并打印出來。
Input:Test case T//T no. of line with the value of N and corresponding values.E.g.24d c b a2 f u1<=T<=1001<=N<=1000Output:Print the power subsets of the set in lexicographic order.Example
例
T=2N=4d c b aOutput:aa ba b ca b c da b da ca c da dbb cb c db dcc dd N=2 f uOutput:ff uuExplanation with example
舉例說明
Let, there is a Set S having N no. of numbers.
令,存在具有N no的集合S。 數字。
S = {X1, X2, X3, ..., Xn}The process to print the subsets of the set is a problem of combination and permutation. To get the result we use the backtracking process.
打印集合子集的過程是組合和排列的問題。 為了獲得結果,我們使用了回溯過程。
First, we will have to sort the character and then we will apply our backtracking procedure.
首先,我們必須對字符進行排序,然后再應用回溯過程。
Let, f(i) = function to insert the ith number into a subset.
設f(i)=將第i 個數字插入子集的函數。
Here, we take a subset of that set in our consideration and consider two things,
在這里,我們考慮一下該集合的子集,并考慮兩件事,
An element is a part of that subset (f(i)).
元素是該子集( f(i) )的一部分。
An element is not a part of that subset (not f(i)).
元素不是該子集的一部分( 不是f(i) )。
Figure 1: Recursion Tree
圖1:遞歸樹
Algorithm:
算法:
Here we use the vector STL to store the subsets.
在這里,我們使用向量STL來存儲子集。
traverse(arr, n, current_pos,set,subset){if(Current_pos is greater or equals to the n)Thenreturnend iffor i= current_pos to the end of the setinsert the element of arr[i] into subsetinsert the subset into the settraverse(arr,n,i+1,set,subset)pop the element from subsetend for}C++ implementation:
C ++實現:
#include <bits/stdc++.h> using namespace std;void traverse(char* arr, int n, int pos, vector<vector<char> >& v, vector<char> vi) {//if the current position is greater than or equals to nif (pos >= n)return;for (int i = pos; i < n; i++) {vi.push_back(arr[i]);v.push_back(vi);traverse(arr, n, i + 1, v, vi);vi.pop_back();} }vector<vector<char> > find_subset(char* arr, int n) {vector<vector<char> > v;vector<char> vi;int pos = 0;traverse(arr, n, pos, v, vi);return v; }void print(vector<vector<char> > v) {for (int i = 0; i < v.size(); i++) {for (int j = 0; j < v[i].size(); j++) {cout << v[i][j] << " ";}cout << endl;} }int main() {int t;cout << "Test Case : ";cin >> t;while (t--) {int n;cout << "Enter the value of n : ";cin >> n;char arr[n];//taking the set elementscout << "Enter the values : ";for (int i = 0; i < n; i++) {cin >> arr[i];}//sort the elementssort(arr, arr + n);vector<vector<char> > v = find_subset(arr, n);//print the vectorprint(v);}return 0; }Output
輸出量
Test Case : 2 Enter the value of n : 4 Enter the values : d c b a a a b a b c a b c d a b d a c a c d a d b b c b c d b d c c d d Enter the value of n : 2 Enter the values : f u f f u u翻譯自: https://www.includehelp.com/icp/power-set-in-lexicographic-order.aspx
python 改變詞典順序
總結
以上是生活随笔為你收集整理的python 改变词典顺序_按词典顺序排列的功率集的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: / 卡路里_最大卡路里
- 下一篇: [转载] python循环中break、