Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuo committed Aug 31, 2018
1 parent a862ea0 commit 6054e07
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
22 changes: 13 additions & 9 deletions AdvancedLevel_C++/1067. Sort with Swap(0,*) (25) .cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ Sample Output:
9

题目大意:给出一个n个数的序列,数字为0~n-1的乱序,每次用两两交换的方式而且只能用0和另一个数交换,使序列变成有序的,问最少需要多少步骤。
分析:贪心算法
1.把0号位置称作公交站,每个数字排序后的位置称作家,每个数字回到正确的位置称作回家
2.只需要把1 ~ n - 1位置上的数字都回家,则排序完成
3.依次遍历每个位置i,如果当前位置不是与之对应的数字,那么我们让这个数字回家
4.数字如何回家:让数字回家前,检查公交站台还有没有人(数字0除外)在公交等待,如果有,让这个人从公交站台直接回家(即交换0号位置和0号数字的家),重复此动作,直到公交站台上的数字为0。然后再次检查i位置上是否等于i这个数字(因为,让公交站台的人回家时,可能会有数字回到此位置),如果不等于,就把此位置的数字放到公交站台等待,为回家做准备
5.在4过程中,如果n-2号位置处理完成后,只剩两种情况,1: 全部有序 ok~ 2: 0号和n-1号互相占着对方的家,把n-1号扔到公交站台就行了(即交换0和n-1号的过程),此过程已经包括在4步骤里了,怕你们不理解,单独说一下~
分析:
1.0号为哨兵, 用用哨兵与其他数字交换,使其他数字回到有序的位置(最后有序时所处的位置),则排序完成
2.a[t] = i; 表示t数字当前正在占着哪一个位置。 (如果想实时监测每个数字的位置,可以用 b 数组{b[a[i]] = i } 缓存一下数据,输出查看的)
3.依次遍历每个位置i,如果当前位置不是与之对应的数字,那么我们让这哨兵来去该数执行操作回到正确位置
4.数字如何被哨兵执行操作回到序的位置:
如果哨兵此时不在自己有序的位置上,那就,先让哨兵去让他占的那个位置上的真正应该放的数字回到此位置,即交换哨兵和此数字,我们swap(a[0],a[a0]),直到哨兵在交换的过程中回到了自己的有序位置。字词再次检查该位置是不是应该放的数字(别的数字回到有序位置的时候即哨兵执行操作的过程中,有可能让此位置该有的数字回到位置)。如果此位置不是当前数字,那哨兵和他交换swap(a[0],a[i]),就是让他先去哨兵的有序位置待一会,等下一轮操作,哨兵会把他交换回来的。如果此位置已经是该数字了,那就什么都不做。
5.当到达最后一个位置时候,两种情况 1)。如果第一个数字和最后一个数字都在自己的有序位置 那ok~ 2).哨兵和最后一个数字互相占着对方的位置,那最后一个数字就是哨兵,交换一次后,哨兵在交换后的位置等待,就是已经回到自己的有序位置,此时排序也是完成的。此过程包括在4里面了,怕你们不理解,单独说一下~

#include <iostream>
using namespace std;
int main() {
int n, cnt = 0, a[100010], id[100010];
int n, t, cnt = 0, a[100010];
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < n; i++){
cin >> t;
a[t] = i;
}
for(int i = 1; i < n; i++) {
if(i != a[i]) {
while(a[0] != 0) {
Expand Down
47 changes: 24 additions & 23 deletions AdvancedLevel_C++/1130. Infix Expression (25).cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Given a syntax tree (binary), you are supposed to output the corresponding infix
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N ( <= 20 ) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:
data left_child right_child
where data is a string of no more than 10 characters, left_child and right_child are the indices of this node's left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by -1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.
where data is a string of no more than 10 characters, left_child and right_child are the indices of this nodes left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by -1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.
Output Specification:
For each case, print in a line the infix expression, with parentheses reflecting the precedences of the operators. Note that there must be no extra parentheses for the final expression, as is shown by the samples. There must be no space between any symbols.
Sample Input 1:
Expand Down Expand Up @@ -32,35 +32,36 @@ Sample Output 2:
(a*2.35)+(-(str%871))

题目大意:给一个二叉树,输出中缀表达式,且加上括号表示运算的优先级~
分析:首先根据所有孩子结点编号寻找1~n中没有出现过的编号标记为root,即树的根结点~然后进行从root结点开始dfs~如果当前index == -1说明当前没有结点则返回空字符串;当right != -1说明该结点不是叶子节点(不可能存在只有左边没有右边的情况的啦,因为那不符合一个算式~左边可以是空表示0~右边不可以的~),那么就将左右结点的val和自身的val串成字符串,保存在自己的val中~如果当前index又不是根结点,那就在左右两边加上括号~最后输出dfs(root)的结果即可~
分析:首先根据所有孩子结点编号寻找1~n中没有出现过的编号标记为root,即树的根结点~然后进行从root结点开始dfs~dfs递归拼接 "(" + 左子树 + 根 + 右子树 + ")"
递归有四种情况(有效的只有三种):
1. 左右子树都空 返回 "(" + 根 + ")"
2. 左空右不空 返回 "(" + 根 + 右子树 + ")"
3. 左不空右空 这种情况不存在
4. 左右都不空 返回 "(" + 左子树 + 根 + 右子树 + ")"
最后递归返回的ans,最外层可能会被括号包起来,也可能不被包起来。要判断一下,如果被包起来,把最外层括号去掉即可~

#include <iostream>
#include <vector>
using namespace std;
struct node {
string val;
int left, right;
};
vector<node> v;
int n, root = 1;
string dfs(int index) {
if (index == -1) return "";
if (v[index].right != -1) {
v[index].val = dfs(v[index].left) + v[index].val + dfs(v[index].right);
if (index != root) v[index].val = '(' + v[index].val + ')';
}
return v[index].val;
string data;
int l, r;
}a[100];
string dfs(int root) {
if(a[root].l == -1 && a[root].r == -1) return a[root].data;
if(a[root].l == -1 && a[root].r != -1) return "(" + a[root].data + dfs(a[root].r) + ")";
if(a[root].l != -1 && a[root].r != -1) return "(" + dfs(a[root].l) + a[root].data + dfs(a[root].r) + ")";
}
int main() {
int have[100] = {0}, n, root = 1;
cin >> n;
v.resize(n + 1);
vector<bool> visit(n + 1, false);
for (int i = 1; i <= n; i++) {
cin >> v[i].val >> v[i].left >> v[i].right;
if (v[i].left != -1) visit[v[i].left] = true;
if (v[i].right != -1) visit[v[i].right] = true;
for(int i = 1; i <= n; i++) {
cin >> a[i].data >> a[i].l >> a[i].r;
if(a[i].l != -1) have[a[i].l] = 1;
if(a[i].r != -1) have[a[i].r] = 1;
}
while (visit[root] == true) root++;
cout << dfs(root) << endl;
while(have[root] == 1) root++;
string ans = dfs(root);
if(ans[0] == '(') ans = ans.substr(1,ans.size()-2);
cout << ans;
return 0;
}

0 comments on commit 6054e07

Please sign in to comment.