ZOJ 1246 Instant Complexity(栈应用)
来源:互联网 发布:mac git ssh key 配置 编辑:程序博客网 时间:2024/05/22 22:27
计算程序时间复杂度的
有可能出现 loop 0的情况
所以给每一个块带一个coe系数,块中如果内嵌块,则把外面的块的coe系数传递过去.
#include <iostream>#include <cstdio>#include <memory.h>#include <vector>#include <string>using namespace std;#define pb push_backconst int maxn=11;bool poly[maxn];int coe[maxn];string buf;struct BLOCK{string bName;int nTimes,cons,coef;BLOCK(const string &bbName="",int nnTimes=0,int ccons=0){bName=bbName,nTimes=nnTimes,cons=ccons;coef=1;}};vector<BLOCK> stak;int main(){int t,cas=1;scanf("%d",&t);while (t--){int f=0;memset(coe,0,sizeof(coe));memset(poly,0,sizeof(poly));while (1){cin>>buf;if(buf=="BEGIN"){BLOCK b(buf);stak.push_back(b);}else if(buf=="LOOP"){char ch;cin>>ch;BLOCK b(buf);if(ch=='n'){b.nTimes=stak.back().nTimes+1;b.coef=stak.back().coef;}else{cin.putback(ch);int tm;cin>>tm;b.nTimes=stak.back().nTimes;b.coef=stak.back().coef*tm;}stak.push_back(b);}else if(buf=="OP"){int tm;cin>>tm;stak.back().cons+=tm;}else if(buf=="END"){BLOCK t=stak.back();stak.pop_back();poly[t.nTimes]=1;coe[t.nTimes]+=t.coef*t.cons;if(t.bName=="BEGIN")break;}}printf("Program #%d\nRuntime = ",cas++);int cnt=0;for (int i=maxn-1;i>=0;--i){if(coe[i]&&poly[i]){if(cnt>0)printf("+");if(i==0&&coe[i]&&poly[i]){printf("%d",coe[i]);}else if(i==1&&coe[i]&&poly[i]){if(coe[i]>1)printf("%d*n",coe[i]);elseprintf("n");}else if(coe[i]&&poly[i]){if(coe[i]>1)printf("%d*n^%d",coe[i],i);elseprintf("n^%d",i);}cnt++;f=1;}}if(f==0){printf("0\n");}else{printf("\n");}printf("\n");}return 0;}
- ZOJ 1246 Instant Complexity(栈应用)
- ZOJ 1246 Instant Complexity
- Instant Complexity
- pku 1472 Instant Complexity
- Poj 1472 Instant Complexity
- POJ1472浅析------Instant Complexity
- POJ 1472 Instant Complexity
- uva 586 Instant Complexity
- UVA 586 Instant Complexity
- POJ 1472 Instant Complexity
- POJ 1472 Instant Complexity
- UVA - 586 Instant Complexity
- poj1472--Instant Complexity(模拟)
- 【POJ】 Instant Complexity (模拟)
- Poj 1472 Instant Complexity
- poj-1472 Instant Complexity
- Instant Complexity - POJ1472
- poj 1472 Instant Complexity
- 我叫坂本我最屌 第一卷.apk
- linux 内核定时器编程
- 1.4.4---Mother's Milk
- 登录SAP的时候 建立快捷方式,密码为灰色的解决办法
- 老工程师的心理话: 给年轻工程师的十大忠告
- ZOJ 1246 Instant Complexity(栈应用)
- TSQL 不用游标的循环方法
- 如何看待地方性棋牌游戏
- java中汉字排序
- decimal.Round
- 解析二进制时,十进制显示为十六进制的数方法
- pip:强大的Python包管理工具
- 手机棋牌的盈利分析
- Eclipse.ini的相关说明