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.
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}

power set in lexocographic order


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 改變詞典順序

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540908.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540908.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540908.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

oracle創建用戶名了,oracle創建用戶名

創建用戶//創建用戶名create user username identified by password‘’//賦權限Grant Connect,Resource,DBA to UserName;plsql developer配置下載地址http://ah1.down.chinaz.com/201011/plsql8.04cn.zip右擊"我的電腦" - "屬性" - "高級" - &…

webpack學習筆記1

webpack學習筆記1&#xff1a;基本概念 前言&#xff1a; 現在在日常的開發中&#xff0c;webpack已經是必不可少的東西了&#xff0c;現在的需求基本都是用webpack對資源進行打包整合&#xff0c;所以打算寫一點關于webpack的東西&#xff0c;這是第一篇&#xff0c;主要是介紹…

ruby 嵌套函數_Ruby嵌套有示例的循環

ruby 嵌套函數嵌套循環 (Nested for loop) Nesting of for loop means one for loop is available inside another for loop. It means that there are two loops, then the first one is an outer loop and the second one is the inner loop. Execution will take place in t…

oracle10數據庫鏈接失敗,Oracle10g出現Enterprise Manager 無法連接到數據庫實例解決辦法...

剛裝好 10g 時&#xff0c;把的監聽端口是1521&#xff0e;后來把端口改成了1568了&#xff0c;登上em發現Enterprise Manager 無法連接到數據庫實例 &#xff0c;連接字符串的端口仍是1521&#xff0c;如何解決這個問題。登陸&#xff1a;出現下面錯誤&#xff1a;Enterprise …

springJdbc in 查詢,Spring namedParameterJdbcTemplate in查詢

springJdbc in 查詢&#xff0c;Spring namedParameterJdbcTemplate in查詢&#xff0c; SpringJdbc命名參數in查詢&#xff0c;namedParameterJdbcTemplate in查詢 >>>>>>>>>>>>>>>>>>>>>>>>>>…

oracle 11g r2版本號,Oracle 11g r2新增版本功能(二)

在11.2中&#xff0c;Oracle數據庫引入的版本的概念&#xff0c;這為應用程序的升級提供了極大的方便。這篇簡單描述版本的實現和查詢方式。前一篇簡單描述了版本&#xff0c;下面接著上面的例子看看Oracle是如何實現這個功能的&#xff1a;SQL> select synonym_name, table…

python 添加圖例_Python | 在圖例標簽中添加Sigma

python 添加圖例Sigma (&#x1d70e;) is very often used greek mathematical letters and has a higher repetition in probability. In this article, we are going to add &#x1d70e; using a command in matplotlib. Sigma(&#x1d70e;)是希臘數學字母中經常使用的字…

【51CTO學院】搜索V2.0——新的搜索,只為給你更好的

為了讓你能快速、準確的找到自己心儀的內容&#xff0c;51CTO學院產品及研發用盡了洪荒之力研發近2個月終于將搜索進行了全面升級。 搜索看似簡單&#xff0c;實則要做到精準和智能卻不是件易事&#xff0c;為了讓學員快速找到自己所需&#xff0c;節省找課時間&#xff0c;更高…

oracle擴容日志文件,ORACLE 加大日志文件

--新建臨時日志文件alter database add logfile group 4 (‘/u01/app/oracle/oradata/orcl/redo04.log‘) size 10m;alter database add logfile group 5 (‘/u01/app/oracle/oradata/orcl/redo05.log‘) size 10m;alter database add logfile group 6 (‘/u01/app/oracle/orad…

java多線程總結(二)

線程一般有6個狀態&#xff1a; 新建狀態&#xff1a;NEW 可運行狀態&#xff1a;RUNNABLE 休眠狀態&#xff1a;TIMED_WAITING 等待狀態&#xff1a;WAITING 阻塞狀態:BLOCKED 終止狀態“TERMINATED 當我們使用new創建線程之后&#xff0c;線程處于新建狀態&#xff0c;當調用…

scandir函數_PHP scandir()函數與示例

scandir函數PHP scandir()函數 (PHP scandir() function) The full form of scandir is "Scan Directory", the function scandir() is used to get the list of the files and directories available in the specified directory. scandir的完整格式為“ Scan Direc…

韓順平.2011最新版.玩轉oracle視頻教程筆記,韓順平.2011最新版.玩轉oracle視頻教程(筆記)...

韓順平.2011最新版.玩轉oracle視頻教程ORA-01045: user XIAOMING lacks CREATE SESSION privilege; logon denied 警告: 您不再連接到 ORACLE。 SQL> show user; USER 為 ""SQL> conn system/p; 已連接。SQL> grant connect to xiaoming; 授權成功。SQL>…

方冪序列 c+~+_C ++編程中的Trigraph序列

方冪序列 c&#xff5e;Trigraph Sequences in C are the set of three characters starting from double question marks (??). These set of the characters replaces by a single character specified in the below table, C 中的Trigraph序列是從雙問號( ?? )開始的三個…

oracle sysauth,sysauth$基表的用戶權限的一點分析

select privilege#,level from sysauth$ connect by grantee#prior privilege# and privilege#>0 start with grantee#:1 and privilege#>0如上的sql語句頻繁執行&#xff0c;其實對于遞歸sql對于自己初始oracle才一年的菜鳥一般是略去不看的&#xff0c;eygle前輩們有時…

ansys 內聚力_內聚力 軟件工程

ansys 內聚力凝聚 (Cohesion) In general terms, the word cohesion means the action or act of forming a united whole. According to the definition of Cambridge University, cohesion is defined as "the state of sticking together, or being in close agreement…

oracle認證都需要考哪幾個方面,Oracle OCP認證要通過哪些考試

Oracle OCP認證要通過哪些考試Oracle OCP DBA認證是所有Oracle認證中最普及的一種認證&#xff0c;這一認證過程是專為那些想要從事Oracle管理的專業數據庫管理人員設計的&#xff0c;適用于Oracle9I DBAs的OCP認證通過改進&#xff0c;刪除了備份和恢復以及網絡考試&#xff0…

左側固定 右側自適應三種方法

第一種&#xff1a;float 單一層浮動法 例如&#xff1a;左側固定成100px; 則核心代碼 左側&#xff1a;width:100px;float:left; 右側 width:auto;margin-left:100px; 實例&#xff1a; <!DOCTYPE html> <html> <head> <meta charset"utf-8&q…

ruby 集合 分組_在Ruby中打印集合的元素

ruby 集合 分組We have gone through the implementation of sets in Ruby. They are very similar to arrays. Now, let us see how we print the elements present in a set. There are no indexes present in the sets because sets are used to store the large elements. …

linux下tmp目錄屬性,Linux:文件夾屬性及umask

回顧&#xff1a;文件在小&#xff0c;也要占用一個Block如&#xff1a;echo > a1.logls a1.log(文件大小為1k)du a1.log(文件大小也應該為1k&#xff0c;如果不是1k&#xff0c;可能selinux是打開的)du -s a1.log文件夾的權限&#xff0c;系統中的文件夾默認權限基本上都為…

python淺復制與深復制_Python中的淺復制與深復制

python淺復制與深復制In python, the assignment operator does not copy the objects, instead, they create bindings between an object and the target. The object, if a collection that is mutable or consists of mutable items uses the copy so that one can change …