Personal programming language Gym

来源:互联网 发布:社交软件英文怎么说 编辑:程序博客网 时间:2024/06/07 02:56


 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;}




原创粉丝点击