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(); } } } /* 求调用的最大嵌套深度所对应的函数名,并输出调用路径, 同时输出这个函数父函数(调用者)的个数和被调用的次数。 同一个函数,如果有多条调用路径都可使其嵌套深度最深,则只输出第一条调用路径 一边专门记 目前深度 + 目前父子关系。 一边专门记 谁 / 几次调用过我。 */
本文作者:MoonOut
本文链接:https://www.cnblogs.com/moonout/p/16455179.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步