Transparency
InterviewTime limit6sMemory limit1024 MB
Given a station graph with uppercase and lowercase edge labels, find the shortest pair of distinct accepted strings whose uppercase letters match, or report -1.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, String
- Solved
- No attempts yet
Problem
You must audit a factory that produces several products. Each product follows its own taxation rules. Working out which rules apply is hard, because the products are difficult to tell apart. Your job is to check whether the factory is completely transparent in its production process, that is, whether it can produce two different products that look the same when lowercase letters are ignored.
Final products are strings of uppercase letters ('A' to 'Z') and lowercase letters ('a' to 'z'). Two final products are indistinguishable if they are equal after all lowercase letters are removed. For example, WabXYcz is indistinguishable from gWXfbY, since removing the lowercase letters leaves WXY in both.
A factory is modelled as a set of stations , a set of directed transition edges with labels, an initial station , and a set of packaging stations . The initial station is in the set of stations, so , and . A transition edge is a 3-tuple , where and is an uppercase or lowercase letter. The edge means that if production is at station , appending to the product leaves it at station . In other words, there is an edge from to labelled .
A product can be produced if, starting at , there is a sequence of edges that ends at a station in and whose labels, concatenated in order, form the product. The sequence can be empty if . For example, the first sample factory can produce AF, FAFB, AbFFAd, and AdydAd. This list is not exhaustive. Each transition can be used an unlimited number of times.
For each station and letter, there is at most one outgoing edge labelled with that letter. That is, implies . A transition may return to the same station, so is allowed.
Given a factory design, determine whether it can produce two distinct but indistinguishable products. If such a pair exists, report the smallest possible sum of the lengths of the two strings in a pair. If no such pair exists, print .
Input
The first line has three integers (), (), and (): the number of stations, the number of packaging stations, and the number of transitions. Stations are numbered 1 to .
Each of the next lines contains one integer (), a packaging station. All values are distinct.
Each of the next lines has the form , meaning a transition from station to station labelled with the letter , where is an uppercase or lowercase letter. The initial station is always station 1. No two transitions leave the same station with the same letter.
Output
Output one integer. If no pair of distinct, indistinguishable products exists, print . Otherwise, print the smallest value of , where and are strings forming such a pair.
Hint
The factory design from the first sample input. Packaging stations are drawn with double circles.
