Labelled Paths
Time limit15sMemory limit1024 MB
Given a DAG whose edge labels are substrings of one string A, find the lexicographically smallest path label from s to every vertex.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Topological sort, String matching, Graph
- Solved
- No attempts yet
Problem
We are given a directed acyclic graph with vertices and edges. Each edge has a label, a string of lowercase letters that may be empty. The label of a path is the concatenation of the edge labels along the path, in path order. The smallest path from a start vertex to a destination vertex is the path whose label is lexicographically smallest among all paths from to . For a given , output the smallest path from to every vertex .
Input
The first line contains four integers , , , and . Here is the length of the string described below. Vertices are numbered from 1 to .
The second line contains the string , exactly lowercase English letters. Every edge label is a substring of .
Each of the next lines contains four integers , , , and . The -th edge goes from to , and its label is the substring of that starts at the -th character and has length . The characters of are indexed from 1 to .
Output
Print lines. The -th line describes the smallest path from to . If no path from to exists, print only the integer 0. Otherwise, print the number of vertices on the path (including and ), followed by the vertices in path order, separated by spaces. If several solutions exist, any of them is accepted.
Constraints
- , , for all
- , , for all
- The graph is acyclic and has no parallel edges. That is, for , or .
Hint
In this example, the edge has label ab and the edge has label a. The smallest path from 3 to 4 is , with label aba.