Personal programming language Gym - 100741B dfs


 d f s ,要存一下在输出

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <ctype.h>
#include <vector>
#include <algorithm>
#include <sstream>
// cout << "  ===  " << endl;

using namespace std;
typedef long long ll;
const int maxn = 10000 + 7;
int n;
map<string, vector<string> > mp;
map<string, string> ans;
stack<string> ans_;
map<string, int> vis;

void init() {
    string str, s, s1;
    for(int i = 0; i < n; ++i) {
        getline(cin, str);
        stringstream ss(str);
        while(ss >> s) {
            if(s == "def") {
                ss >> s1;
            }
            else if(s == "with") {
                ss >> s;
                mp[s1].push_back(s);
            }
            else if(s == "=") {
                ss >> s;
                ans[s1] = s;
            }
        }
    }
}

void dfs(string s) {
    if(mp[s].size() == 0) {
        if(vis[s] == 0) {
            ans_.push(ans[s]);
            vis[s]++;
        }
        return;
    }
    for(int i = mp[s].size()-1; i >= 0; --i) {
        if(!vis[mp[s][i]])dfs(mp[s][i]);
    }
    if(vis[s] == 0) {
        ans_.push(ans[s]);
        vis[s]++;
    }
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    cin.get();
    init();
    string s;
    cin >> s;
    dfs(s);
    string t = ans_.top(); ans_.pop();
    cout << t;
    while(!ans_.empty()) {
        t = ans_.top(); ans_.pop();
        cout << " " << t;
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值