uva 586 Instant Complexity
来源:互联网 发布:linux 文件后面带星号 编辑:程序博客网 时间:2024/04/30 04:59
点击打开链接uva 586
思路:递归模拟
分析:1 题目是一道给定一段程序代码的球时间复杂度
2 根据题目的意思,我们可以利用栈和递归的方法,但是栈的方法比较不好写,所以我们利用递归的思路来写
3 当我们遇到LOOP的时候,我们就递归下去,当遇到OP的时候我们就去计算当前这一层的复杂度,遇到END的时间retuen即可
4 注意最后的输出
代码:
#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 15;int ans[MAXN];char str[MAXN];void getAns(vector<string>v , int x){ int size = v.size(); int num_n = 0; int num_xishu = x; for(int i = 0 ; i < size ; i++){ int len = v[i].size(); if(v[i][0] == 'n') num_n++; else{ int sum = 0; for(int j = 0 ; j < len ; j++) sum = sum*10 + v[i][j]-'0'; num_xishu *= sum; } } ans[num_n] += num_xishu;}void solve(vector<string>v){ int x; while(1){ scanf("%s" , str); if(str[0] == 'L'){ scanf("%s" , str); v.push_back(str); solve(v); v.pop_back(); } else if(str[0] == 'O'){ scanf("%d" , &x); getAns(v , x); } else if(str[0] == 'E'){ return; } }}void output(){ printf("Runtime = "); bool isFirst = true; bool isTime = false; for(int i = MAXN-1 ; i >= 1 ; i--){ if(ans[i]){ isTime = true; if(isFirst) isFirst = false; else printf("+"); if(ans[i] > 1) printf("%d*" , ans[i]); printf("n"); if(i > 1) printf("^%d" , i); } } if(ans[0]){ isTime = true; if(!isFirst) printf("+"); printf("%d" , ans[0]); } if(!isTime) printf("0"); printf("\n\n");}int main(){ int Case , cas = 1; vector<string>v; scanf("%d" , &Case); while(Case--){ scanf("%s" , str); memset(ans , 0 , sizeof(ans)); printf("Program #%d\n" , cas++); v.clear(); solve(v); output(); } return 0;}
- uva 586 Instant Complexity
- UVA 586 Instant Complexity
- UVA - 586 Instant Complexity
- UVA 586 - Instant Complexity(dfs)
- uva 586 - Instant Complexity(dfs)
- Instant Complexity
- pku 1472 Instant Complexity
- Poj 1472 Instant Complexity
- POJ1472浅析------Instant Complexity
- POJ 1472 Instant Complexity
- POJ 1472 Instant Complexity
- POJ 1472 Instant Complexity
- ZOJ 1246 Instant Complexity
- poj1472--Instant Complexity(模拟)
- 【POJ】 Instant Complexity (模拟)
- Poj 1472 Instant Complexity
- poj-1472 Instant Complexity
- Instant Complexity - POJ1472
- hdu 4612 Warm up
- TOP PS aux时显示出来的栏目代表什么意思?比如:RSS、VSZ、STAT等
- poj 2528 Mayor's posters
- javascript如何解析特殊类型的json数据?
- Python文件读写时的换行符与回车符
- uva 586 Instant Complexity
- Android下利用spydroid+dss 实现多看一视频监控
- 杭电oj2003
- hdu 1867 KMP A + B for you again
- 规则引擎Drools解决汽水问题
- VC读写XML
- Block
- 正则表达式补充学习
- OpenCV 2 遇到问题汇总