Happy Tree Friend!(递归模拟)
来源:互联网 发布:淘宝的优质网店 编辑:程序博客网 时间:2024/05/29 07:42
解题思路:vector+结构体 模拟,递归输出。调试了很久,注意|–(此处有空格)name,前面空格每4个一组。
#include <iostream>#include <algorithm>#include <vector>#include <string>using namespace std;const int Maxn = 1005;typedef struct Node{ string name; vector<int> Vec;};Node node[Maxn];string stringStruct;bool cmp(const int& a, const int& b){ return node[a].name<node[b].name;}void Print(int id, bool last){ cout << node[id].name << endl; if(node[id].Vec.size()==0) return; sort(node[id].Vec.begin(), node[id].Vec.end(), cmp); if(id>0) { stringStruct+="| "; if(last) stringStruct[stringStruct.length()-4] = ' '; } for(int i = 0; i < node[id].Vec.size(); ++i) { if(i==node[id].Vec.size()-1) { cout << stringStruct << "`-- "; Print(node[id].Vec[i], true); } else { cout << stringStruct << "|-- "; Print(node[id].Vec[i], false); } } if(id>0) stringStruct = stringStruct.substr(0, stringStruct.length()-4);}int main(){ int n, fa; string stringStruct; while(cin >> n) { for(int i = 0; i < n; ++i) node[i].Vec.clear(); for(int i = 0; i < n; ++i) { cin >> node[i].name >> fa; node[fa].Vec.push_back(i); } Print(0, true); } return 0;}/*10my-app -1src 0main 1java 2resource 2webapp 2test 1java 6resource 6pom.xml 0*/
阅读全文
0 0
- Happy Tree Friend!(递归模拟)
- Happy birthday to my dear friend--xiaoqiong
- HDU6152 Friend-Graph【模拟】
- TOJ 4117 Happy tree friends
- [树链剖分] CF593D. Happy Tree Party
- [哈希&&模拟]Happy Number uva10591
- bnu 34988 Happy Reversal(模拟)
- leetcode 202. Happy Number 模拟
- 【启发】94. Binary Tree Inorder Traversal(树的非递归遍历 + 栈模拟递归)
- Tree 递归
- zoj 3327 Friend Number 模拟题
- ZOJ 3327Friend Number(模拟题)
- friend
- friend
- Friend
- Friend
- friend
- friend
- 在EXCEL中快速输入数据的方法
- Python中文字输出乱码
- leetcode 50. Pow(x, n) 一个简单的分治算法的应用
- Python爬虫(一):爬取单页面
- static详解
- Happy Tree Friend!(递归模拟)
- nodejs中req里使用的东西
- HR面试,你需要注意什么?
- aop aspectj [intellij idea配置] [hello world]
- WSGI简介
- C++Primer--string、vector、iterator
- MongoDB入门篇(一)
- 系统启动流程
- VS链接提示无法解析的外部符号 __imp__fopen解决办法