Random Walk

Time limit1sMemory limit128 MB

Summary
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 xx; if x>0.3x > 0.3 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:

  1. Reserved words (case sensitive): NOP, IF, GOTO, END, PROC, PROG_START, PROG_END.
  2. A program begins with PROG_START and ends with PROG_END.
  3. A program contains one or more procedures.
  4. Each procedure begins with PROC [name], where [name] consists only of letters and digits and is never a reserved word.
  5. A procedure ends with END;.
  6. A procedure has one or more commands (END; is not a command). Executing one command takes exactly one time unit.
  7. 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 xx uniformly from [0,1)[0, 1); if x>x > threshold, execute [line] next, otherwise execute the next line.
    • IF x<[threshold] GOTO [line]; — draw xx; if x<x < threshold, execute [line] next, otherwise execute the next line.
    • IF x>[threshold] PROC [name]; — draw xx; if x>x > 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 xx; if x<x < threshold, run procedure [name] in full, then return to the following line; otherwise continue to the next line.
  8. A procedure finishes as soon as its END; is reached.
  9. 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:

  1. Every IF condition compares the random variable with a constant threshold.
  2. The random variables in different IF statements are independent — even two identical IF x>0.5 commands use independent draws.
  3. There is no (direct or indirect) mutual recursion between procedures.
  4. 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.
  5. A program has more than 1 and fewer than 100 procedures.
  6. 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.

Examples4

  1. Example 1

    Input
    PROG_START
    PROC A
    IF x>0.5 GOTO 3;
    NOP;
    END;
    PROC B
    IF x<0.5 PROC A;
    NOP;
    END;
    PROG_END
    B
    A
    REQUEST_END
    
    Expected output
    2.750
    1.500
    
  2. Example 2

    Input
    PROG_START
    PROC Main
    NOP;
    NOP;
    NOP;
    END;
    PROG_END
    Main
    REQUEST_END
    
    Expected output
    3.000
    
  3. Example 3

    Input
    PROG_START
    PROC Loop
    IF x>0.5 GOTO 1;
    END;
    PROG_END
    Loop
    REQUEST_END
    
    Expected output
    2.000
    
  4. Example 4

    Input
    PROG_START
    PROC C
    IF x<0.25 GOTO 1;
    NOP;
    END;
    PROG_END
    C
    REQUEST_END
    
    Expected output
    2.333