【Luogu】【关卡2-14】 树形数据结构(2017年10月)【AK】
任务说明:由一个根节点分叉,越分越多,就成了树。树可以表示数据之间的从属关系
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】的更多相关文章
- 【Luogu】【关卡2-6】贪心(2017年10月)
任务说明:贪心就是只考虑眼前的利益.对于我们人生来说太贪是不好的,不过oi中,有时是对的. P1090 合并果子 有N堆果子,只能两两合并,每合并一次消耗的体力是两堆果子的权重和,问最小消耗多少体力. ...
- 【Luogu】【关卡2-3】排序(2017年10月) 【AK】
任务说明:将杂乱无章的数据变得有规律.有各种各样的排序算法,看情况使用. 这里有空还是把各种排序算法总结下吧.qsort需要会写.. P1177 [模板]快速排序 这个题目懒得写了,直接sort了.. ...
- 欢迎来怼-Alpha周(2017年10月19)贡献分配规则和分配结果
.从alpha周(2017年10月19日开始的2周)开始,提高贡献分比重. 贡献分 : 团队分 = 1 : 5 教师会在核算每位同学总分时按比例乘以系数. 每位同学带入团队贡献分10分,如果团队一共7 ...
- 2017年10月31日结束Outlook 2007与Office 365的连接
2017 年10月31日 ,微软即将推出 Office 365中Exchange Online邮箱将需要Outlook for Windows的连接,即通过HTTP Over MAPI方式,传统使用R ...
- 江西省移动物联网发展战略新闻发布会举行-2017年10月江西IDC排行榜与发展报告
编者按:当人们在做技术创新时,我们在做“外包产业“:当人们在做制造产业,我们在做”服务产业“:江人们在做AI智能时,我们在做”物联网“崛起,即使有一个落差,但红色热土从不缺少成长激情. 本期摘自上月初 ...
- 【Luogu】【关卡2-13】线性数据结构(2017年10月)【还差一道题】
任务说明:数组,链表,队列,栈,都是线性结构.巧用这些结构可以做出不少方便的事情. P1996 约瑟夫问题 n个人,排成环形,喊到m的人出列,输出出列顺序. 咳咳,这个题目不好写,尽管简单就是模拟题. ...
- 【Luogu】【关卡1-8】BOSS战-入门综合练习2(2017年10月)【AK】------都是基础题
P1426 小鱼会有危险吗 我个人觉得这个题目出的不好,没说明白,就先只粘贴的AC代码吧 #include <bits/stdc++.h> using namespace std; int ...
- 【Luogu】【关卡2-16】线性动态规划(2017年10月)【还差三道题】
任务说明:这也是基础的动态规划.是在线性结构上面的动态规划,一定要掌握. P1020 导弹拦截 导弹拦截 P1091 合唱队形 老师给同学们排合唱队形.N位同学站成一排,音乐老师要请其中的(N-K)位 ...
- 【Luogu】【关卡2-15】动态规划的背包问题(2017年10月)【还差一道题】
任务说明:这是最基础的动态规划.不过如果是第一次接触会有些难以理解.加油闯过这个坎. 01背包二维数组优化成滚动数组的时候有坑有坑有坑!!!必须要downto,downto,downto 情景和代码见 ...
随机推荐
- 【LeetCode】Array
[11] Container With Most Water [Medium] O(n^2)的暴力解法直接TLE. 正确的解法是Two Pointers. O(n)的复杂度.保持两个指针i,j:分别指 ...
- 力扣 ——4Sum (四数之和)python 实现
题目描述: 中文: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 targe ...
- Android功耗评测系列之——软件评测方案原理
软件评测方案也有很多种,但核心原理都是同一个. 软件评测方案中,所有Android功耗的统计都是通过代码进行估算,没有任何实体电路和硬件设备参与统计汇报. 这个配置文件存储在Android ...
- Python自动补全缩写意义
自动补全的变量的类别p:parameter 参数 m:method 方法(类实例方法)调用方式classA aa.method()或者classA().method() c:class 类 v:var ...
- Pycharm文档模板变量
点击这里查看JetBrains官方英文源文件 本篇Blog只是搬运外加大概翻译一下. File template variables A file template can contain varia ...
- pc上用C语言模拟51多任务的案例程序
#include <stdio.h> #include <stdlib.h> //任务槽个数.在本例中并未考虑任务换入换出,所以实际运行的任务有多少个, //就定义多少个任务槽 ...
- FTPClient TLS 与 FTP 进行数据传输异常:Remote host closed connection during handshake
环境:java JDK 1.8.org.apache.commons-net-3.6.jar.端口已放开 FTPClient ftpClient = new FTPClient(protocol, f ...
- Mysql 列变行其中一种做法。
需求是: 上班打卡记录 和 下班打卡记录 是分别是两条数据,现在是要合并为一条数据,并且封装成一个实体. 有可能是 只有上班记录,,或者是只有下班的记录.如何关联全查,一边为null或者另一边 ...
- PHP curl_multi_info_read函数
curl_multi_info_read — 获取当前解析的cURL的相关传输信息 说明 array curl_multi_info_read ( resource $mh [, int &$ ...
- MySQL图形化管理工具之Navicat安装以及激活
软件以及激活包下载地址 1. 安装navicat 双击navicat_trial_11.1.20.0.1449226634.exe,一路下一步安装(记住安装目录) 2. 激活 双击PatchNavic ...