Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2157 | Accepted: 747 |
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
2 BEGIN LOOP n OP 4 LOOP 3 LOOP n OP 1 END OP 2 END OP 1 END OP 17 END BEGIN OP 1997 LOOP n LOOP n OP 1 END END END
Sample Output
Program #1 Runtime = 3*n^2+11*n+17 Program #2 Runtime = n^2+1997
input
2
BEGIN OP
17
LOOP 2
LOOP n
LOOP
2 OP
4 LOOP
n OP 4
LOOP n OP 5
END
END OP 4 END
END END
END
BEGIN
OP 0 LOOP n LOOP
n
OP 88 OP 0 LOOP n LOOP 0 OP 17 END END
END END
OP 0 LOOP n LOOP 3 OP 0 END
END OP 8
END
output
Program #1
Runtime = 20*n^3+16*n^2+32*n+17
Program #2
Runtime = 88*n^2+8
///附上两组测试数据
题意 :算时间复杂度
loop x就是for循环 x可能是n可能是非负数
op x(x是非负数)就是 时间复杂度x
注意x可能为0
直接递归模拟 begin 视为loop 1
#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
using namespace std;
#define For(i,a,b) for(i=a;i<=b;i++)
#define _For(i,a,b) for(i=b;i>=a;i--)
#define Out(x) cout<<x<<endl
#define Outdouble(x,a) cout<<fixed<<setprecision(a)<<1.0*x<<endl
#define pf printf
#define sf scanf
#define mset(arr,num) memset(arr,num,sizeof(arr))
#define ll long long
const ll inf = 3000010; ///
#define ok std::ios::sync_with_stdio(0)
#pragma comment(linker, "/STACK:102400000,102400000")
// #define debug
#if defined (debug)
---check---
#endif
/// ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ///
void Plus(int temp[],int answer[]) ///后面的数组加上前面的数组
{
int i;
For(i,0,10)
{
answer[i] = answer[i] + temp[i];
}
}
ll trs(char s[]) ///判断到底是数还是字母
{
if(s[0] == 'n')
{
return -1;
}
int i;
ll key = 0;
for(i=0; s[i]; i++)
{
key = key*10 + s[i] - '0';
}
return key;
}
void loop(int answer[],char loopcur[]) ///answer是这次循环的时间复杂度 loopcur 是循环数
{
int k;
string p;
char tempLoopcur[110];
mset(tempLoopcur,0);
int tempAnswer[20];
while(1)
{
cin>>p;
if(p[0] == 'L')
{
cin>>tempLoopcur;
mset(tempAnswer,0); ///tempAnswer清零 记录上一次循环的时间复杂度
loop(tempAnswer,tempLoopcur);
Plus(tempAnswer,answer); ///加到当前的answer
}
else if(p[0] == 'O')
{
int x;
cin>>x;
answer[0]+=x;
}
else if(p[0] == 'E')
{
///tempanswer中有数
int temp = trs(loopcur);
if(temp != -1) ///说明是数
{
For(k,0,10)
{
if(answer[k] != 0)
{
answer[k]*=temp;
}
}
}
else ///是n
{
_For(k,0,10)
{
if(answer[k] != 0)
{
answer[k+1] = answer[k];
answer[k] = 0;
}
}
}
return ;
}
}
}
void output(int num,int answer[]) ///输出很恶心 按照权值循环
{
int k;
bool flag = 0;
pf("Program #%d\nRuntime = ",num);
_For(k,0,10)
{
if(answer[k] != 0)
{
if(flag)
{
pf("+");
}
if(k==0)
{
pf("%d",answer[k]);
}
else if(k==1)
{
if(answer[k] == 1)
{
pf("n");
}
else
{
pf("%d*n",answer[k]);
}
}
else if(answer[k] == 1)
{
pf("n^%d",k);
}
else
{
pf("%d*n^%d",answer[k],k);
}
flag = 1;
}
}
if(flag == 0)
{
pf("0");
}
pf("\n\n");
}
int main()
{
int n,num=0;
ok;
string s;
cin>>n;
while(n--)
{
num++;
int answer[20];
mset(answer,0);
cin>>s;
loop(answer,"1"); ///最后返回到answer 按照权值存储
output(num,answer);
}
return 0;
}