Random Walk
Time limit1sMemory limit128 MB
Parse a small random program with procedures and threshold IF/GOTO or PROC commands, then compute each requested procedure's expected running time to three decimals.
- Level
Medium7 of 10
- Topics
- Probability, Graph, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
Random algorithms are widely used today — for example in cryptography (primality testing, and so on). Unlike ordinary deterministic algorithms, a random algorithm need not follow a fixed execution trace: at certain points it "flips a coin" and decides where to go based on the result.
For instance, consider a point like this:
x = random(1); // random(1) returns a real number drawn uniformly from [0, 1)
IF x > 0.3 GOTO ...
When the program reaches this point it draws a random ; if it jumps elsewhere, otherwise it continues straight ahead. The program wanders like a drunk walking down the street — you cannot predict its path in advance.
Your task is to build a simple Random Program Evaluator that computes the expected running time of chosen procedures.
A simple random program is defined as follows:
- Reserved words (case sensitive):
NOP,IF,GOTO,END,PROC,PROG_START,PROG_END. - A program begins with
PROG_STARTand ends withPROG_END. - A program contains one or more procedures.
- Each procedure begins with
PROC [name], where[name]consists only of letters and digits and is never a reserved word. - A procedure ends with
END;. - A procedure has one or more commands (
END;is not a command). Executing one command takes exactly one time unit. - A command has one of the following forms:
NOP;— continue to the next line on the next time unit.IF x>[threshold] GOTO [line];— draw uniformly from ; if threshold, execute[line]next, otherwise execute the next line.IF x<[threshold] GOTO [line];— draw ; if threshold, execute[line]next, otherwise execute the next line.IF x>[threshold] PROC [name];— draw ; if threshold, run procedure[name]in full, then return to the line following this command; otherwise continue to the next line.IF x<[threshold] PROC [name];— draw ; if threshold, run procedure[name]in full, then return to the following line; otherwise continue to the next line.
- A procedure finishes as soon as its
END;is reached. - Within each procedure line numbers start at 1: the first command is line 1, the second is line 2, and so on;
END;is the last line.
To keep things simple:
- Every
IFcondition compares the random variable with a constant threshold. - The random variables in different
IFstatements are independent — even two identicalIF x>0.5commands use independent draws. - There is no (direct or indirect) mutual recursion between procedures.
- From every command, the probability of eventually reaching the end of its procedure is positive, so the end is always reachable and every expected time is finite.
- A program has more than 1 and fewer than 100 procedures.
- Every string in the problem is at most 100 characters.
Report each expected running time rounded to exactly 3 digits after the decimal point.
Input
The input consists of one random program (as described above) with one or more procedures, followed by a list of requests. Each request is a line holding the name of a procedure whose expected running time must be reported. The request list ends with a line REQUEST_END (no procedure is named REQUEST_END).
Output
For each request, print one line with the expected running time of the requested procedure, rounded to 3 digits after the decimal point.