2020北航计算机夏令营机试 T2 函数调用

题意及测试用例:

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9+7;

int a, b, prime[5000], cnt=0;
map<string, set<string>> called;
map<string, int> call_num;

int main(){
    ios::sync_with_stdio(false);
    int type; string func; stack<string> st;
    int maxi=0; vector<stack<string>> max_stack;
    while(cin>>type) {
        if(type==-1){
            for(auto st : max_stack){
                int len = st.size(); string fff = st.top();
                cout<<fff<<" "; st.pop();
                stack<string> temp; while(!st.empty()) temp.push(st.top()), st.pop();
                for(int i=0; i<len-2; ++i) cout<<temp.top()<<"-", temp.pop();
                cout<<temp.top()<<" ";
                cout<<called[fff].size()<<" ";
                cout<<call_num[fff]<<endl;
            }
            break;
        }
        if(type==1){
            cin>>func;
            if(call_num.count(func)==0) call_num[func]=0;
            if(called.count(func)==0) called[func]=set<string>();
            // call num
            call_num[func] = call_num[func] + 1;
            // who calls me
            if(!st.empty()){
                called[func].insert(st.top());
            }
            // the path
            st.push(func);
            if(st.size()>maxi){
                maxi = st.size();
                max_stack.clear();
                max_stack.push_back(stack<string>(st));
            }
            else if(st.size()==maxi){
                bool ok=true;
                for(auto sss : max_stack)
                    if(sss.top()==func) {ok=false; break;}
                if(ok)
                    max_stack.push_back(stack<string>(st));
            }
        }
        else{
            st.pop();
        }
    }
}

/*
求调用的最大嵌套深度所对应的函数名,并输出调用路径,
同时输出这个函数父函数(调用者)的个数和被调用的次数。
同一个函数,如果有多条调用路径都可使其嵌套深度最深,则只输出第一条调用路径

一边专门记 目前深度 + 目前父子关系。
一边专门记 谁 / 几次调用过我。

*/

posted @ 2022-07-07 16:32  MoonOut  阅读(106)  评论(0)    收藏  举报