递归-文件结构“图”(算法基础 第3周)

文件结构“图”

这里写图片描述
这里写图片描述
分析:
呃,,,挺不好写的,搞不定,本来想优化一下参考代码,还是挺麻烦的。
参考: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值