TopCoder problem "LinearCity" used in TCHS SRM 39 (Division I Level Two)



Problem Statement

    

In some cities, when you ask for the location of a place, people give you a specific address. LinearCity, on the other hand, is one-dimensional, so people just tell you to walk either left or right from your current location until you reach your desired location.



Over time, you have collected information about the relative positions of pairs of places in LinearCity. This information is given in the String refDirection and the int[]s refSource and refDestination. The i-th element of refDirection is the direction you need to walk (either 'L' or 'R', quotes for clarity) to get from refSource[i] to refDestination[i] (each place in the city is represented by a distinct integer between 0 and N-1, inclusive, where N is the total number of places in the city). You know that all the information is consistent, meaning that there is no place that is both to the left and to the right of another place.



You are arranging a tour for newcomers to the city, and you need to know the directions between various places. Your are given int[]s source and destination, each containing the same number of elements. Return a String[] where the i-th element is the direction you must walk to get from source[i] to destination[i]. If the direction cannot be deduced, the corresponding element should be "UNKNOWN" (quotes for clarity). Otherwise, it should be either "LEFT" of "RIGHT" (quotes for clarity).



 

Definition

    
Class:LinearCity
Method:getReference
Parameters:int[], int[], String, int, int[], int[]
Returns:String[]
Method signature:String[] getReference(int[] refSource, int[] refDestination, String refDirection, int N, int[] source, int[] destination)
(be sure your method is public)
    
 

Constraints

-refSource will contain between 1 and 50 elements, inclusive.

-Each element of refSource will be between 0 and N-1, inclusive.

-refDestination will contain the same number of elements as refSource.

-Each element of refDestination will be between 0 and N-1, inclusive.

-refSource[i] will be different than refDestination[i], for all i between 0 and M-1, where M is the size of refSource.

-refDirection will contain the same number of characters as the number of elements in refSource.

-Each character in refDirection will be 'L' or 'R'.

-N will be between 2 and 50, inclusive.

-source will contain between 1 and 50 elements, inclusive.

-Each element of source will be between 0 and N-1, inclusive.

-destination will contain the same number of elements as source.

-Each element of destination will be between 0 and N-1, inclusive.

-source[i] will be different than destination[i], for all i between 0 and K-1, where K is the size of source.

-All references will be consistent, as explained in the problem statement.

 

Examples

0)
    
{1, 2}
{2, 0}
"RR"
3
{1, 0}
{0, 1}
Returns: {"RIGHT", "LEFT" }
If you can go from 1 to 2 walking to the right and you can go from 2 to 0 walking to the right, then you can go from 1 to 0 walking to the right passing by 2. Finally, if you can go from 1 to 0 walking to the right, then you can go from 0 to 1 walking to the left.
1)
    
{1, 0}
{2, 2}
"RL"
3
{1, 0}
{0, 1}
Returns: {"RIGHT", "LEFT" }
This is the same case as Example 0, but with the direction between 0 and 2 reversed.
2)
    
{2, 3, 1, 0, 2, 0, 5, 5}
{1, 4, 4, 4, 4, 3, 2, 3}
"RLRLRLLL"
6
{0, 1, 0}
{2, 3, 5}
Returns: {"LEFT", "RIGHT", "UNKNOWN" }
You can go from 0 to 2 walking to left passing through places 3, 4 and 1, in order. Similarly, you can go from 1 to 3 walking to the right passing through place 4. However, it is impossible to find a unidirectional path from 0 to 5 using the given references.
3)
    
{1, 0, 2, 3}
{0, 2, 3, 2}
"RRRL"
5
{0, 2, 3, 0, 4}
{2, 4, 1, 1, 0}
Returns: {"RIGHT", "UNKNOWN", "LEFT", "LEFT", "UNKNOWN" }
It is possible to have repeated references, and to have a query about a place not mentioned in the given references.
4)
    
{6, 0, 0, 5, 2, 4, 1, 1, 1, 6, 2, 0, 2, 2, 3, 1, 5, 1, 5, 6, 0}
{4, 6, 4, 2, 6, 3, 2, 4, 5, 5, 3, 1, 0, 4, 0, 6, 4, 3, 3, 3, 5}
"RLRLRRRRRRRLRRLRRRRRL"
7
{5, 6, 2, 4, 6, 2, 4}
{0, 0, 0, 5, 2, 5, 6}
Returns: {"RIGHT", "RIGHT", "RIGHT", "LEFT", "LEFT", "RIGHT", "LEFT" }

Problem url:

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

Problem stats url:

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

Writer:

jbernadas

Testers:

PabloGilberto , brett1479 , Olexiy , ivan_metelsky

Problem categories:

Graph Theory