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,

在这里,我们考虑一下该集合的子集,并考虑两件事,

  1. An element is a part of that subset (f(i)).

    元素是该子集( f(i) )的一部分。

  2. 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 改变词典顺序

python 改变词典顺序_按词典顺序排列的功率集相关推荐

  1. 看python源代码的顺序_查看“Python-2020-fall”的源代码

    因为以下原因,您没有权限编辑本页: 您所请求的操作仅限于该用户组的用户使用:用户 您可以查看与复制此页面的源代码.== Python程序设计课程主页(2020年秋季学期) == Teacher: [h ...

  2. python数据模块下载顺序_对Python random模块打乱数组顺序的实例讲解

    在我们使用一些数据的过程中,我们想要打乱数组内数据的顺序但不改变数据本身,可以通过改变索引值来实现,也就是将索引值重新随机排列,然后生成新的数组.功能主要由python中random模块的sample ...

  3. python作用域的顺序_“Python”函数的参数和范围,及其,作用域

    1. 参数类型 1.1 位置参数 显然对位置和顺序有要求,形参和实参必须一一对应,不可或缺 def show(msg): print(msg) s = "I love you!" ...

  4. python方法解析顺序_浅谈Python的方法解析顺序(MRO)

    方法解析顺序, Method Resolution Order 从一段代码开始 考虑下面的情况: class A(object): def foo(self): print('A.foo()') cl ...

  5. python关键字参数顺序_位置参数和关键字参数的求值顺序

    Python 2.7 如果我们看一下与为函数调用创建AST(^{})相关的CPython源代码,那么参数求值的顺序是:return Call(func, args, keywords, vararg, ...

  6. python嵌套循环执行顺序_两个嵌套for循环的执行顺序

    展开全部 当两个或2113多个循环语句嵌套时,执行5261顺序按照一下步骤: 1.先判断最外4102层循环条件,若1653满足条件则进入第一层循环体. 2.进入第一层循环体后再次遇到循环语句进行第二层 ...

  7. python 语句执行顺序_一个针对 Python 语句执行顺序的练习

    摘自 Fluent Python evalsupport.py print(' evalsupport module start') def deco_alpha(cls): print(' deco ...

  8. python改变图像颜色_通过python改变图片特定区域的颜色详解

    首先让我祭出一张数学王子高斯的照片,这位印在德国马克上的神人有多牛呢? 他是近代数学的奠基人之一,与牛顿, 阿基米德并称顶级三大数学家,随便找一个编程语言的数学库,里面一定有和他名字相关的一堆函数. ...

  9. python改变像素点颜色_在python中更改像素的颜色

    要在图像上绘制正方形,可以使用matplotlib中的Rectangle.在import numpy as np import matplotlib.pyplot as plt import matp ...

最新文章

  1. Java打造一款SSH客户端,而且已开源
  2. 架构师之路 — 软件架构 — 软件版本定义
  3. Excel较大规模数据处理实例(可直接用)python实现
  4. 机器学习之手把手实现第1部分:支持向量机的原理和实现
  5. 昆山立讯电子工程师_教会徒弟饿死师傅?立讯精密会不会成为第二个富士康
  6. C++工作笔记-对容器模板的初步认识
  7. 百度SEO万能网页操作编程者 v2.0
  8. sscli 2.0 简介
  9. azure 使用_如何使用Cloud Shell自动化Azure Active Directory(AAD)任务
  10. PDE34 Transport equation: derivation general solution
  11. Android Open Source Projects(汇总与整理)
  12. Java集合框架篇-64-TreeSet集合练习题2
  13. thinkpad使用u盘启动
  14. 我将出席 .NET Day in China 的圆桌讨论:探讨开发者就业话题
  15. 立体栅格地图_三维栅格地图构建之三:点集到栅格的投射
  16. RS BCH级联编译码的性能仿真
  17. win7开机后资源管理器未响应或者停止问题
  18. analy32.xll下载_Android Studio 4.0添加了Motion Editor和Build Analyzer
  19. Aurora 8b/10b AXI4-ST回环测试
  20. Retrofit 2.0 超能实践(一),okHttp完美支持Https传输

热门文章

  1. [2020-CVPR] Dynamic Region-Aware Convolution 论文简析
  2. mysql 账户管理_Mysql账户管理原理与实现方法详解
  3. clocks_per_sec 时间不正确_你该拥有的不只是护肤品,还有正确护肤时间表
  4. python 成员函数 泛型函数_【一点资讯】白学这么多年 Python?连泛型函数都不会写? www.yidianzixun.com...
  5. origin设置不同区域的颜色_[测试狗]Origin入门教程(二十四):效率翻倍小技巧——修改默认字体...
  6. linux下qq怎么截图,ubuntu 12.04使用QQ截图安装教程
  7. 将z-blog改成英文blog所遇到的问题
  8. Redis Sentinel 模拟故障迁移
  9. 0623TP框架联系
  10. SQLServer中的死锁的介绍