任务说明:由一个根节点分叉,越分越多,就成了树。树可以表示数据之间的从属关系

P1087 FBI树

给一个01字符串,0对应B,1对应I,F对应既有0子节点又有1子节点的根节点,输出这棵树的后序遍历。字符串长度小于等于2^10。

心情好,写代码一次ac了

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <map>
#include <vector>
#include <string> using namespace std; void init(string& str) {
if (str.empty()) {
return;
}
if (str.size() == ) {
if (str == "") { cout << "B"; }
else if (str == "") { cout << "I"; }
return;
}
string s1 = str.substr(, str.size()/);
string s2 = str.substr(str.size()/);
init(s1);
init(s2);
if (str.find("") != string::npos && str.find("") != string::npos) {
cout << "F";
} else if (str.find("") != string::npos) {
cout << "B";
} else {
cout << "I";
}
return;
} int main() {
int n;
cin >> n;
string str;
cin >> str;
init(str);
cout << endl;
//system("pause");
return ;
}

 P1030 求先序排列

给两个字符串,表示一棵树的中序遍历和后序遍历,要求输出这棵树的先序遍历。

我是模拟了这棵树,先建树,然后先序遍历的。

但是还可以有更好的解法,不用建树,直接dfs就ok!!!!

 #include <iostream>
#include <cstdio>
#include <string> using namespace std; struct TreeNode {
char val;
TreeNode* left = nullptr;
TreeNode* right = nullptr;
}; TreeNode* initTree(string& str1, string& str2) {
if (str1.empty() && str2.empty()) {
return nullptr;
}
char c = str2[str2.size()-];
TreeNode* root = new(TreeNode);
root->val = c;
int str1Pos = str1.find(c);
string str1Left = str1.substr(, str1Pos); string str1Right = str1.substr(str1Pos + ); string str2Left = str2.substr(, str1Pos); string str2Right = str2.substr(str1Pos, str1Right.size()); root->left = initTree(str1Left, str2Left);
root->right = initTree(str1Right, str2Right);
return root;
} string strans = "";
void visitTree(TreeNode* root) {
if (!root) { return; }
strans += root->val;
visitTree(root->left);
visitTree(root->right);
return;
} int main() {
string s1, s2;
cin >> s1 >> s2;
TreeNode* root = initTree(s1, s2);
visitTree(root);
cout << strans << endl;
return ;
}

P1305 新二叉树

这个题目写segment fault了,要用gdb看堆栈....orz

Linix 查看core文件配置方法如下: http://www.jianshu.com/p/5549a6e71a1d

怎么用gdb看堆栈,看内存这里还是需要强化...最后发现问题的方法还是输出屏幕看log。。orz

segment fault 的原因是:

 //没有判断top是否存在就pop了,踩内存了orz
while ( stk.top() == '*') { stk.pop(); } //wrong
while (!stk.empty() && stk.top() == '*') { stk.pop(); } //correct

以后要注意,判断一个值的时候一定要先写这个值能不能存在orz

题目是:输入一串二叉树,用遍历前序打出。

输入格式:

第一行为二叉树的节点数n。

后面n行,每一个字母为节点,后两个字母分别为其左右儿子。

空节点用*表示

输出格式:前序排列的二叉树

想法就是先找根节点,然后用个栈来保存左右儿子。左儿子先pop,然后递归的找左儿子的儿子进栈。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <map>
#include <vector> using namespace std; int n;
map<char, int> mp;
stack<char> stk; int main() {
cin >> n;
vector<vector<char>> info(n, vector<char>());
for (int i = ; i < n; ++i) {
getchar();
//scanf("%c%c%c", &info[i][0], &info[i][1], &info[i][2]);
cin >> info[i][] >> info[i][] >> info[i][];
mp[info[i][]]++, mp[info[i][]]++, mp[info[i][]]++; }
char rootval = '=';
for (auto ele : mp) {
if (ele.second == ) {
rootval = ele.first;
}
}
stk.push(rootval);
int times = ;
while(!stk.empty()) {
while (!stk.empty() && stk.top() == '*') { stk.pop(); }
if (stk.empty()) { break; }
char ch = stk.top();
stk.pop();
for (int i = ; i < n; ++i) {
if (info[i][] == ch) {
stk.push(info[i][]);
stk.push(info[i][]);
}
}
cout << ch ;
}
return ;
}

【Luogu】【关卡2-14】 树形数据结构(2017年10月)【AK】的更多相关文章

  1. 【Luogu】【关卡2-6】贪心(2017年10月)

    任务说明:贪心就是只考虑眼前的利益.对于我们人生来说太贪是不好的,不过oi中,有时是对的. P1090 合并果子 有N堆果子,只能两两合并,每合并一次消耗的体力是两堆果子的权重和,问最小消耗多少体力. ...

  2. 【Luogu】【关卡2-3】排序(2017年10月) 【AK】

    任务说明:将杂乱无章的数据变得有规律.有各种各样的排序算法,看情况使用. 这里有空还是把各种排序算法总结下吧.qsort需要会写.. P1177 [模板]快速排序 这个题目懒得写了,直接sort了.. ...

  3. 欢迎来怼-Alpha周(2017年10月19)贡献分配规则和分配结果

    .从alpha周(2017年10月19日开始的2周)开始,提高贡献分比重. 贡献分 : 团队分 = 1 : 5 教师会在核算每位同学总分时按比例乘以系数. 每位同学带入团队贡献分10分,如果团队一共7 ...

  4. 2017年10月31日结束Outlook 2007与Office 365的连接

    2017 年10月31日 ,微软即将推出 Office 365中Exchange Online邮箱将需要Outlook for Windows的连接,即通过HTTP Over MAPI方式,传统使用R ...

  5. 江西省移动物联网发展战略新闻发布会举行-2017年10月江西IDC排行榜与发展报告

    编者按:当人们在做技术创新时,我们在做“外包产业“:当人们在做制造产业,我们在做”服务产业“:江人们在做AI智能时,我们在做”物联网“崛起,即使有一个落差,但红色热土从不缺少成长激情. 本期摘自上月初 ...

  6. 【Luogu】【关卡2-13】线性数据结构(2017年10月)【还差一道题】

    任务说明:数组,链表,队列,栈,都是线性结构.巧用这些结构可以做出不少方便的事情. P1996 约瑟夫问题 n个人,排成环形,喊到m的人出列,输出出列顺序. 咳咳,这个题目不好写,尽管简单就是模拟题. ...

  7. 【Luogu】【关卡1-8】BOSS战-入门综合练习2(2017年10月)【AK】------都是基础题

    P1426 小鱼会有危险吗 我个人觉得这个题目出的不好,没说明白,就先只粘贴的AC代码吧 #include <bits/stdc++.h> using namespace std; int ...

  8. 【Luogu】【关卡2-16】线性动态规划(2017年10月)【还差三道题】

    任务说明:这也是基础的动态规划.是在线性结构上面的动态规划,一定要掌握. P1020 导弹拦截 导弹拦截 P1091 合唱队形 老师给同学们排合唱队形.N位同学站成一排,音乐老师要请其中的(N-K)位 ...

  9. 【Luogu】【关卡2-15】动态规划的背包问题(2017年10月)【还差一道题】

    任务说明:这是最基础的动态规划.不过如果是第一次接触会有些难以理解.加油闯过这个坎. 01背包二维数组优化成滚动数组的时候有坑有坑有坑!!!必须要downto,downto,downto 情景和代码见 ...

随机推荐

  1. day01 html介绍 文档声明头 head标签 body标签

    day01 html   初识html <!--文档的声明--> <!doctype html>   <html lang="en">    # ...

  2. String 字符串和StringBuffer的知识点总结

    String字符串 1  字符串.equals();                                                   equals和length的区别:equals ...

  3. vue-router中的router-link的active-class

    vue-router中的router-link的active-class   在vue-router中要使用选中样式的方法有两种: 1.直接在路由js文件中配置linkActiveClass 2.在r ...

  4. codeblocks编译调试C语言二级指针小记

    夜已深,暂时附上一个截图,后面慢慢道来. 下图时用codeblocks调试C语言的界面,codeblocks版本是17.12nosetup版,也为继承mingw,我用的编程器是tdm-gcc-5.1. ...

  5. 【leetcode】519. Random Flip Matrix

    题目如下: You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix whe ...

  6. BZOJ 3105: [cqoi2013]新Nim游戏(线性基)

    解题思路 \(nim\)游戏先手必胜的条件是异或和不为\(0\),也就是说第一个人拿走了若干堆后不管第二个人怎么拿都不能将剩余堆的异或和变成\(0\).考虑线性基,其实就是每个数对线性基都有贡献,任何 ...

  7. AcWing 225. 矩阵幂求和 (矩阵快速幂+分治)打卡

    题目:https://www.acwing.com/problem/content/227/ 题意:给你n,k,m,然后输入一个n阶矩阵A,让你求  S=A+A^2+A^3.+......+A^k 思 ...

  8. 在 MacBook 上安装 Ubuntu

    建立ubuntu 安裝U盤,加入usb port,按住option key 開機,正常安裝. 完成後,會缺少wifi drive sudo apt-get install bcmwl-kernel-s ...

  9. PHP基础知识总结(二) 数据类型、数组、变量和运算符

    一.PHP是服务器端的脚本语言.在服务器端解析完成后,向前端浏览器发送html. PHP文件的部署位置在:xampp\htdocs文件夹下,当然这个位置可以修改.二.PHP基本语法 1.文件后缀名是p ...

  10. 90、Tensorflow实现分布式学习,多台电脑,多个GPU 异步试学习

    ''' Created on 2017年5月28日 @author: weizhen ''' import time import tensorflow as tf from tensorflow.e ...