TopCoder problem "WebBrowser" used in TCHS SRM 39 (Division I Level One)



Problem Statement

    

You and your team are programming a web browser for internal use by your employer. The team leader has assigned you the task of programming the logic of the address toolbar and the back/forward navigation buttons.



The web browser always stores two sequences of web pages - the back pages sequence and the forward pages sequence. It also stores the single current page (unless there is no page currently loaded). The browser should respond the user actions encoded as follows (all quotes for clarity only):

  • "x": If the web page x is the current page, this command is completely ignored. Otherwise, add the current page (if any) to the end of the back pages sequence. Then, load x as the current page and remove all pages from the forward pages sequence.
  • "BACK": If the back pages sequence is empty, this command is completely ignored. Otherwise, add the current page (if any) to the beginning of the forward pages sequence. Remove the last page from the back pages sequence and load it as the current page.
  • "FORWARD": If the forward pages sequence is empty, this command is completely ignored. Otherwise, add the current page (if any) to the end of the back pages sequence. Remove the first page from the forward pages sequence and load it as the current page.

To test the component, you must make a function that takes a String[] containing a sequence of user actions, and returns the sequence of pages the browser should load. At the beginning of the test, the back and forward sequences are empty and there is no page currently loaded.

 

Definition

    
Class:WebBrowser
Method:getSequence
Parameters:String[]
Returns:String[]
Method signature:String[] getSequence(String[] actions)
(be sure your method is public)
    
 

Constraints

-actions will contain between 1 and 50 elements, inclusive.
-Each element in actions will be either "BACK", "FORWARD" or "x", where x is the name of a page to visit.
-Each web page name will contain between 1 and 50 characters, inclusive, and will be composed only from lowercase letters ('a'-'z') and dot characters ('.').
 

Examples

0)
    
{"BACK", "BACK", "FORWARD", "FORWARD"}
Returns: { }
No page needs to be loaded, because no page visit command is issued.
1)
    
{"a.com", "b.com", "c.com", "BACK", "BACK", "BACK",
 "FORWARD", "FORWARD", "FORWARD"}
Returns: {"a.com", "b.com", "c.com", "b.com", "a.com", "b.com", "c.com" }
There may be more BACK or FORWARD commands than those that can be executed.
2)
    
{"a.com", "b.com", "BACK", "c.com", "BACK", "FORWARD"}
Returns: {"a.com", "b.com", "a.com", "c.com", "a.com", "c.com" }
When "c.com" is loaded, the browser forgets "b.com" as a forward page.
3)
    
{"a.com", "b.com", "b.com", "BACK"}
Returns: {"a.com", "b.com", "a.com" }
The second "b.com" command is ignored, so the "BACK" command goes to "a.com".
4)
    
{"a.com", "b.com", "BACK", "a.com", "FORWARD"}
Returns: {"a.com", "b.com", "a.com", "b.com" }
The second "a.com" command is ignored, so b.com is not ignored as a forward page.
5)
    
{"ag", "BACK", "bh", "BACK", "af", "bp", "BACK", "FORWARD",
 "ao", "BACK", "bo", "FORWARD", "BACK", "BACK", "ah"}
Returns: {"ag", "bh", "ag", "af", "bp", "af", "bp", "ao", "bp", "bo", "bp", "af", "ah" }

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8121

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10782&pm=8121

Writer:

jbernadas

Testers:

PabloGilberto , brett1479 , Olexiy , ivan_metelsky

Problem categories:

Simulation