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.
2
4
d c b a
2
f u
1<=T<=100
1<=N<=1000
Output:
Print the power subsets of the set in lexicographic order.
Example
例
T=2
N=4
d c b a
Output:
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
N=2
f u
Output:
f
f u
u
Explanation 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) )。
For N=3
S = {a b c}
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)Then
return
end if
for i= current_pos to the end of the set
insert the element of arr[i] into subset
insert the subset into the set
traverse(arr,n,i+1,set,subset)
pop the element from subset
end 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 n
if (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 elements
cout << "Enter the values : ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
//sort the elements
sort(arr, arr + n);
vector<vector<char> > v = find_subset(arr, n);
//print the vector
print(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 改变词典顺序