Instant Complexity--模拟
来源:互联网 发布:做好网络舆情工作 编辑:程序博客网 时间:2024/06/05 15:56
Description
Generally, one determines the run-time of an algorithm in relation to the `size' n of the input, which could be the number of objects to be sorted, the number of points in a given polygon, and so on. Since determining a formula dependent on n for the run-time of an algorithm is no easy task, it would be great if this could be automated. Unfortunately, this is not possible in general, but in this problem we will consider programs of a very simple nature, for which it is possible. Our programs are built according to the following rules (given in BNF), where < number > can be any non-negative integer:
< Program > ::= "BEGIN" < Statementlist > "END" < Statementlist > ::= < Statement > | < Statement > < Statementlist > < Statement > ::= < LOOP-Statement > | < OP-Statement > < LOOP-Statement > ::= < LOOP-Header > < Statementlist > "END" < LOOP-Header > ::= "LOOP" < number > | "LOOP n" < OP-Statement > ::= "OP" < number >
The run-time of such a program can be computed as follows: the execution of an OP-statement costs as many time-units as its parameter specifies. The statement list enclosed by a LOOP-statement is executed as many times as the parameter of the statement indicates, i.e., the given constant number of times, if a number is given, and n times, if n is given. The run-time of a statement list is the sum of the times of its constituent parts. The total run-time therefore generally depends on n.
Input
Output
Output a blank line after each test case.
Sample Input
2BEGIN LOOP n OP 4 LOOP 3 LOOP n OP 1 END OP 2 END OP 1 END OP 17ENDBEGIN OP 1997 LOOP n LOOP n OP 1 END ENDEND
Sample Output
Program #1Runtime = 3*n^2+11*n+17Program #2Runtime = n^2+1997题目链接:http://poj.org/problem?id=1472
变态模拟,我再一次搜了题解。。。。太变态的模拟了,大神就是厉害。
以下转载至:http://blog.csdn.net/lyy289065406/article/details/6648640
大致题意:
给出一段Pascial程序,计算其时间复杂度(能计算的项则计算,不能计算则化到最简的关于n的表达式O(n),并把各项根据n的指数从高到低排列),输出时,系数为0的项不输出,系数为1的项不输出系数,指数为1的项不输出指数。
一段程序只有唯一一个BEGIN,代表程序的开始。与其对应的为最后的END,代表程序的结束。
一段程序最多只有10层循环嵌套,循环的入口为LOOP,一个LOOP对应一个END,代表该层循环的结束。
一段程序中OP的个数不限。
LOOP是循环的入口,其后面的数据可能是常量(非负整数),也可能是变量n,代表循环体执行的次数。
OP是语句,其后面的数据只能为常量(非负整数),代表该语句执行的次数。
解题思路:
此题就是一条表达式化简的模拟题,用递归直接模拟。
以第一个样例说明处理方法:
BEGIN
LOOP n
OP 4
LOOP 3
LOOP n
OP 1
END
OP 2
END
OP 1
END
OP 17
END
从该例子我们可以得到一条关于n的最初表达式:
n*(4+3*(n*1+2)+1)+17
稍微化简一下,合并同一层的OP值,得到了
n*(3*(n*1+2)+5)+17
不难看出每一个循环体都能写成k*n+i形式的子表达式,其中loop是*关系,op是+关系
由于最大循环次数为10,那么我们用exp[11]存储多项式的每一项的指数i和系数k=exp[i],其中exp[0]其实就是常数项,由OP语句产生
注意LOOP后面可能输入字符n,也可能输入数字,处理方法:用字符串输入s,若为s[0]==n,则直接作字符处理;若s[0]!=n,则认为是数字串,把它转换为int再处理。
恩,正在膜拜中。。。
代码:
//Memory Time //264K 0MS #include<iostream>using namespace std;/*字符串转数字*/int StrToNum(char* s){int digit=0;for(int i=0;s[i];i++)digit=digit*10+(s[i]-'0');return digit;}bool solve(int* exp){char s[30];cin>>s;if(s[0]=='E') //ENDreturn false;else if(s[0]=='B') //BEGINwhile(solve(exp)); //若因为OP返回,则继续;若因为END返回,则结束else if(s[0]=='O') //0P{cin>>s;exp[0]+=StrToNum(s);return solve(exp);}else //LOOP{int TempExp[11]={0}; //临时exp[]cin>>s;while(solve(TempExp));if(s[0]=='n') //LOOP n{for(int i=10;i>0;i--)TempExp[i]=TempExp[i-1]; //表达式乘以n,则所有项的次数+1TempExp[0]=0;}else //LOOP Num{int x=StrToNum(s);for(int i=0;i<11;i++)TempExp[i]*=x; //表达式乘以const,则所有项的系数*const}for(int i=0;i<11;i++)exp[i]+=TempExp[i];}return true;}int main(void){int test;cin>>test;for(int t=1;t<=test;t++){char s[6];int exp[11]={0}; //指数为i的项,其系数为exp[i]solve(exp);cout<<"Program #"<<t<<endl;cout<<"Runtime = ";bool flag=false;bool before=false; //标记输出当前项之前,是否输出过前面的项for(int i=10;i>=0;i--)if(exp[i]) //当系数不为0时,才输出该项{flag=true;if(before){cout<<'+';before=false;}if(!i) //当指数为0时,直接输出系数{cout<<exp[i];before=false;}else{bool sign=false; //标记系数是否为1if(i && exp[i]!=1){sign=true;cout<<exp[i];before=true;}if(i) //当指数不为0时{if(sign)cout<<'*';cout<<'n';if(i!=1)cout<<'^'<<i;before=true;}}}if(!flag)cout<<0<<endl<<endl;elsecout<<endl<<endl;}return 0;}
- poj1472--Instant Complexity(模拟)
- 【POJ】 Instant Complexity (模拟)
- Instant Complexity--模拟
- Instant Complexity(dfs 模拟)
- uva586 - Instant Complexity 模拟循环
- Instant Complexity poj1472 (递归模拟)
- UVA586 - Instant Complexity(递归加模拟)
- POJ 1472:Instant Complexity 模拟时间复杂度
- POJ 1472Instant Complexity(模拟+dfs)
- Instant Complexity
- POJ 1472 Instant Complexity(大模拟 判断程序执行次数)
- poj 1472 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
- 各种编程语言的应用范围
- 使用Slider来构建Splash欢迎界面
- Spring框架内容整理(一):IOC
- WebService的两种方式SOAP和REST比较
- CocoaPods 安装 使用
- Instant Complexity--模拟
- PHP基本类型boolean
- 一道题构建二叉树编译不同的思考
- Android 自动更新之状态栏下载状态和进度
- 读书感悟
- SQLite(数据库)简析
- bzoj 3956: Count (单调栈+st表)
- 单独HornetQ启动HornetQ
- C语言基本教程 第13课:标准库函数的使用