VK Cup 2015 - Finals, online mirror
A
Matching Names
Codeforces
2300
原题
←B →
翻译中…

Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names.

翻译中…

There are $*n*$ students in a summer school. Teachers chose exactly $*n*$ pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym $*b*$ to a student with name $*a*$ as the length of the largest common prefix $*a*$ and $*b*$. We will represent such value as image. Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students.

翻译中…

Find the matching between students and pseudonyms with the maximum quality.

翻译中…

Input

翻译中…

The first line contains number $*n*$ ($1 ≤ *n* ≤ 100 000$) — the number of students in the summer school.

翻译中…

Next $*n*$ lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.

翻译中…

The last $*n*$ lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating.

翻译中…

The total length of all the names and pseudonyms doesn't exceed $800 000$ characters.

翻译中…

Output

翻译中…

In the first line print the maximum possible quality of matching pseudonyms to students.

翻译中…

In the next $*n*$ lines describe the optimal matching. Each line must have the form $*a*$ $*b*$ ($1 ≤ *a*, *b* ≤ *n*$), that means that the student who was number $*a*$ in the input, must match to the pseudonym number $*b*$ in the input.

翻译中…

The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.

翻译中…

Note

翻译中…

The first test from the statement the match looks as follows:

翻译中…
  • bill $→$ bilbo (lcp = 3)
  • galya $→$ galadriel (lcp = 3)
  • gennady $→$ gendalf (lcp = 3)
  • toshik $→$ torin (lcp = 2)
  • boris $→$ smaug (lcp = 0)
翻译中…

样例

Input #1
5
gennady
galya
boris
bill
toshik
bilbo
torin
gendalf
smaug
galadriel
Output #1
11
4 1
2 5
1 3
5 2
3 4
InputCopy OutputCopy
题目信息
题号
566A
时间限制
2 s
内存限制
256 MB
标签
DFS 及类似
字符串
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。