您的位置:首页 > 其它

递归-文件结构“图”(算法基础 第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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: