Under construction

Still being built. Things may change

Foosball Dynasty

Time limit2sMemory limit512 MB

Summary
Simulate a foosball variation where seats rotate after each point and report the team whose scoring streak lasted longest.
Level

Medium5 of 10

Topics
Simulation, Implementation, Queue, Array
Solved
No attempts yet

Problem

Balaji and his coworkers play foosball on their lunch break. The game normally takes two players or four, but so many coworkers wanted in that Balaji invented a variation for five or more. Every point is played by four people: two on the White team and two on the Black team. Each team has one Offense player and one Defense player.

Everyone else waits in a line for a turn. After each point the table rearranges itself.

  1. The two players on the scoring team swap positions with each other. If White scores, the White Offense player becomes the White Defense player and the White Defense player becomes the White Offense player.
  2. The Offense player of the team that lost the point moves to Defense on the same team.
  3. The Defense player of the team that lost the point leaves the table and goes to the back of the line.
  4. The person at the front of the line joins the table as the new Offense player of the team that lost the point.

While one team keeps scoring, its two players stay at the table and only trade positions with each other after every point. The run ends the moment the other team scores. Each maximal run of consecutive points won by the same team is a dynasty for that pair. A dynasty can be a single point or a long streak. The winners of this variation are the players who build the longest dynasty.

Given the starting seats and the result of every point, find the longest dynasty.

Input

The first line contains an integer nn, the number of players (5≤n≤105 \le n \le 10).

The second line contains the nn names of the players, separated by spaces. The first four are the four people who arrive at the table first, in the order they arrive. The first starts at White Offense, the second at Black Offense, the third at White Defense, and the fourth at Black Defense. The rest get in line in the given order and wait.

The third line contains a non-empty string of at most 10001000 characters recording which side scored each point, in order. 'W' means the White team scored the point and 'B' means the Black team scored it.

Output

Display the team that achieved the longest dynasty. If several dynasties tie for the longest, display each of those teams on its own line, in the order the dynasties happened.

On a line, write the two names separated by a space, with the player who sat down at the table earlier first. Use the most recent time each player took a seat, not an earlier stint. For the four starting players, the order given in the input is that order. The same pair can appear on more than one line, and the two names can appear in a different order on each of those lines.

Examples3

  1. Example 1

    Input
    6
    Balaji David Alex Scott Andrew Ravi
    WWBWBBWBW
    
    Expected output
    Balaji Alex
    Andrew David
    
  2. Example 2

    Input
    6
    Amy Jinu Kasey Sarah Sheetal Julia
    BBBBB
    
    Expected output
    Jinu Sarah
    
  3. Example 3

    Input
    5
    Ada Ben Cleo Dmitri Elif
    W
    
    Expected output
    Ada Cleo