Subway Fares
InterviewTime limit1sMemory limit128 MB
Given a per-stop fare table and an ordered list of station names, find the number of stops between two stations and print the corresponding fare.
- Level
Easy3 of 10
- Topics
- Array, String, Implementation, Hash map
- Solved
- No attempts yet
Problem
One of the first questions a subway traveler asks is: how much will the trip cost? Presenting this clearly should be simple, yet in practice it can mean looking up several tables posted around the station and then doing awkward arithmetic. Let's write a program that computes it instead.
We focus on a single subway line. You are given a price table stating how much a trip of stops costs, for each . You are also given the names of the stations in order, together with your starting and ending station. Compute the fare. Note that the line can be traveled in either direction, so only the number of stops between the two stations matters.
Input
The first line contains the number of data sets. Each of the data sets has the following form.
The first line of a data set contains an integer (), the number of stops on this line. The next lines describe the price table: the -th of these lines contains an integer, the price of traveling stops ( means getting off at the stop immediately after boarding). The next lines each contain the name of a station, listed in order along the line; every name consists of lowercase letters only and all names are distinct.
The last two lines contain the name of your starting station and the name of your ending station. Both appear in the station list and differ from each other.
Output
For each data set, print the line Data Set x:, where x is the number of the data set (starting from 1), followed by a line containing the fare for the trip. Separate consecutive data sets with a single blank line.