递归-文件结构“图”(算法基础 第3周)
2016-06-07 20:42
381 查看
文件结构“图”
分析:
呃,,,挺不好写的,搞不定,本来想优化一下参考代码,还是挺麻烦的。
参考:http://acbingo.cn/2016/04/01/coursera_%E7%AE%97%E6%B3%95_%E9%80%92%E5%BD%92/
源码:
#include <iostream> #include <vector> #include <string> #include <queue> using namespace std; //函数对象 class cmp { public: bool operator()(string a, string b) { return a>b; } }; void print(int deep, string s) { for(int i=0; i<deep; i++) cout << "| "; cout << s << endl; } int fun(int deep, string s) { priority_queue<string, vector<string>, cmp> q; if (deep == 0) { cout << "ROOT" << endl; if (s[0] == 'f') q.push(s); if (s[0] == 'd') fun(deep+1, s); if (s[0]==']' || s[0]=='*') { while(!q.empty()) { print(deep, q.top()); q.pop(); } return 1; } } else print(deep, s); while(cin >> s) { if(s[0]=='f') q.push(s); if(s[0]=='d') fun(deep+1, s); if(s[0]==']' || s[0]=='*') { while(!q.empty()) { print(deep, q.top()); q.pop(); } if (s[0]=='*') cout << endl; return 1; } } } int main() { int t=0; while(1) { string s; cin >> s; if (s[0] == '#') return 0; t++; cout << "DATA SET " << t << ':' << endl; fun(0, s); } return 0; }
相关文章推荐
- 【THUSC2016】蒟蒻的酱油记
- 最近十次打字记录
- 跟着小白学~如何使用二维数组打印杨辉三角
- canvas text
- linux下vi操作Found a swap file by the name
- Lightoj 1003 - Drunk(拓扑排序)
- 静态储存
- C++智能指针(auro_ptr...)
- 优先队列用法
- 实战 virtualenv 搭建多版本 Python 环境
- laravel随记(3)
- C++智能指针(auro_ptr...)
- PB打印标签DLL,支持128码( 未测)
- hibernate基于泛型基础增删改查方法封装
- MySql中UTF8 和 GBK 编码中文字符长度问题
- block 块函数
- 实在没有好的缩放VIEW,只能实现
- spark 文件系统Alluxio(之前名为Tachyon)
- 项目管理:沟通与沟通计划
- ABAP CHECK, EXIT, RETURN, LEAVE PROGRAM 用法