【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 情景和代码见 ...
随机推荐
- 2018-8-10-win10-uwp-httpClient-登陆CSDN
title author date CreateTime categories win10 uwp httpClient 登陆CSDN lindexi 2018-08-10 19:16:53 +080 ...
- matlab直接运行fig文件时报错
Matlab里面所的程序都是以.m的脚本文件形式保存的,所有运行的都是m文件.所以,对于guide生成的GUI程序,打开的方式有两种: 一:打开.m文件,点击m文件上的运行按钮,会自动弹出figure ...
- react app相关知识
1.快速新建名为hello-world项目的应用命令 npx create-react-app hello-world 2.使用serve来mock数据 ①先安装serve npm i ...
- <自动化测试>之<unittest框架使用1>
要说单元测试和UI自动化之间的是什么样的一个关系,说说我个人的一些心得体会吧,我并没有太多的这方面经验,由于工作本身就用的少,还有就是功能测试点点对于我这种比较懒惰的人来说,比单元测试复杂...思考单 ...
- idea 使用github
[Toc] #一.首先下载github for window 客户端,或者git客户端 这里只演示gitHub客户端,安装git客户端的话,git.exe很容易找得到. 附上网址:https://de ...
- Ruby 技能图谱
# Ruby 技能图谱 说明: 本图谱只捡重点的列举,并非包含全部.文中所列举或没有列举的资源信息都可以在[awesome-ruby](https://github.com/markets/aweso ...
- Codeforces 578B "Or" Game (前缀和 + 贪心)
Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] 题目链接:B. "Or" Game You are given \(n\) ...
- shell awk匹配字符串(从配置文件)
配置文件 config.properties xxx_yyy_lib_path="路径" xxx_yyy_bin_path="路径" 想通过shell来读入路径 ...
- 3.Jmeter 快速入门教程(三-1) --添加响应断言(即loadrunner中所指的检查点)
上一节课,我们创建了一个测试场景,并进行了少量vuser的负载测试. 有时候我们执行了测试,但是发现并不是所有事务都执行成功了. 那是因为我们只是发起了测试,但并没有对每次请求测试的返回作校验. 所以 ...
- spring配置mybatis的sqlsessionfactory
<!--读入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.bean ...