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 中国大陆许可协议进行许可。

posted @   MoonOut  阅读(87)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Sibelius: Violin Concerto in D Minor, Op. 47:III. Allegro, ma non tanto Jascha Heifetz / Chicago Symphony Orchestra
Sibelius: Violin Concerto in D Minor, Op. 47:III. Allegro, ma non tanto - Jascha Heifetz / Chicago Symphony Orchestra
00:00 / 00:00
An audio error has occurred.