400399: GYM100162 F Longest Two Graphs Common String
Description
You are given two directed graphs G1 and G2. The vertices of both graphs are labeled with lowercase English letters. Different vertices can have the same labels, each vertex is labeled with exactly one letter.
Moving along a path in a graph one may pronounce a string. So each path v1, v2, ..., vk in a graph (where (vi, vi + 1) is an arc for each 1 ≤ i < k) generates a string "lv1lv2... lvk", where lv is the label of the vertex v. A path may contain cycles, it can degenerate to a single vertex.
One may pronounce set of strings using G1 and set of strings using G2. What is the length of the longest common string in two sets? Write a program which will find the required length and the corresponding paths in both graphs.
InputThe input contains several test cases. Each test case consists of description of G1 and description of G2. Each description starts with the line containing integers n and m (1 ≤ n ≤ 500, 0 ≤ m ≤ 4000), where n is the number of vertices and m is the number of arcs. The next line contains a string of exactly n lowercase English letters, the i-th letter in the string stands for the label of the i-th vertex. The following m lines contain arcs, one arc description per line. Each description is a pair of integers xj, yj (1 ≤ xj, yj ≤ n) — the starting and finishing endpoints of the j-th arc. Graphs can contain loops and multiple arcs.
The test cases are separated with a blank line.
OutputFor each test case display the case number and the length of the longest common string. If the longest common string is infinite, display "-1" as the length. In the case of finite longest common string, display two additional lines. Lines should contain vertex indices along the corresponding path in G1 and G2. If there are multiple answers, display any of them.
ExamplesInput6 5Output
abacab
1 2
2 3
3 4
4 5
5 6
5 5
aabac
1 2
2 3
3 4
4 5
5 1
1 1
a
1 1
2 2
aa
1 2
2 1
Case 1: 5
1 2 3 4 5
2 3 4 5 1
Case 2: -1