Time Limit Exceeded
Time limit1sMemory limit128 MB
Parse nested loops with arguments x, y, or integers, compute the Big-O polynomial of basic operations, and print terms sorted by degree.
- Level
Medium7 of 10
- Topics
- Stack, Implementation, Math, Sorting
- Solved
- No attempts yet
Problem
While solving problems, you have probably seen a "Time Limit Exceeded" message. It happens for many reasons, but most often when the time complexity of the submitted code exceeds the maximum the problem allows.
In this problem, let's deepen our understanding of time complexity so we run into fewer time-limit errors. Given a piece of source code, compute its time complexity.
Analyzing real code directly is hard, so we use the following simplified model. Assume a program consists of only four instructions:
basic: a basic operation such as arithmetic or an assignmentloop: the start of a loopendloop: the end of a loopendprogram: the end of the program
Analysis follows these rules:
- Only the four instructions above appear in a program.
- Every
looptakes exactly one argument and pairs with the firstendloopencountered after it to form one loop. - A
loopargument isx,y, or a positive integer.xandyare constants that never change during execution. The loop repeats that many times. - If a
loopcontains nobasicat all, it is a meaningless loop that terminates immediately regardless of its argument (it contributes nothing to the execution count). - If a
loopcontains severalbasicinstructions, you may treat it as containing just one. - Executing one
basictakes constant time.
The time complexity is the number of basic executions expressed as a function of x and y, then simplified with Big-O notation.
Big-O is defined as follows: if you can choose positive constants and such that holds for every input at least , then the Big-O of is . Informally, all constant factors can be dropped.
For example, the Big-O of is . Also, when a higher-degree term is present, any lower-degree term can be removed entirely: the Big-O of is , and of is .
However, when different variables are mixed and a term cannot be guaranteed smaller than the others, every such term must be kept. For example, the Big-O of is , and the Big-O of is .
Input
The first line contains the number of test cases . Each test case (program) is separated by a blank line.
Each program consists only of the four instructions described above, and exactly one endprogram appears below the outermost loop.
There is exactly one space between loop and its argument; apart from that, no extra whitespace or other characters appear in a program. Loops are nested at most levels deep.
Output
For each test case, first print Data Set K: (where is the 1-based test case number), then print the time complexity of that program.
Print the terms in order of decreasing degree of x; when two terms have the same degree of x, print the one with the higher degree of y first.
Abbreviate each term as much as possible: print xy instead of x^1y^1, and x instead of x^1y^0. If the complexity is constant, print 1; if basic never executes, print 0. Join multiple terms with +.
Print one blank line between consecutive test cases.