Computer Science

Interview

Time limit1sMemory limit128 MB

Summary
Score each page for a query word using occurrences in the page and in linking pages weighted by word distance to the hyperlink, then print the highest scoring pages.
Level

Medium6 of 10

Topics
Implementation, String, Simulation, Array
Solved
No attempts yet

Problem

Computer science studies ways of solving real-world problems using computers and related technology, and it covers a great many topics. One of its recent success stories is organizing and searching information — just think of how web search has changed everyone's life. Here we explore a very simple web search engine.

You are given several documents made of words and hyperlinks (in a format simpler than HTML). You are also given queries (one word each) and must find the most relevant results. A page PP is relevant either because (1) the query word appears in PP, or because (2) the query word appears in a page that links to PP, close to that hyperlink.

Scoring. Let qq be the query word.

  • PP earns one point for every occurrence of qq in PP.
  • For every hyperlink LL that goes from some page P′P' to PP, and for every occurrence of qq in P′P' at word distance dd from LL, page PP earns max⁡(4−d,0)\max(4 - d, 0) points — that is, 4−d4 - d points when d<4d < 4, and 00 points otherwise.

The displayed word of a hyperlink is itself a word of the document: it counts as an occurrence when it equals qq, and it occupies its own position when distances are measured. In particular, if qq is the displayed word of the hyperlink LL itself, that occurrence is at distance d=0d = 0 and is worth 44 points. Distances are counted in words over the whole document, ignoring line breaks. The same occurrence in P′P' may be counted several times if it is close to several hyperlinks pointing to PP.

Note: reason (2) is often the more important one. What other pages say about you when they link to you is frequently a better description than what you say about yourself — partly because a page could otherwise spam its own text, and partly because a page often does not contain every relevant term.

Input

The first line contains an integer K≥1K \ge 1, the number of data sets. It is followed by KK data sets of the following form.

The first line of a data set contains two integers mm and nn: the number of queries and the number of web pages. Both are between 11 and 100100.

The next mm lines each contain one query: a single word of at most 2020 lowercase letters.

Then come the descriptions of the nn web pages. The description of page ii starts with a line containing the number ℓi\ell_i of lines that page ii consists of (between 11 and 100100), followed by ℓi\ell_i lines of text of at most 255255 characters each. Each line of text is a sequence of words separated by single spaces. Every word consists only of lowercase letters and is at most 2020 characters long.

Some words are hyperlinks, written by enclosing the displayed word together with the target page number in square brackets. For example, [usc,3] is a hyperlink whose displayed word is usc and whose target is page 33. The target is always a valid page between 11 and nn, and there are never self-links (a page never links to itself).

Output

For each data set, first print a line Data Set x:, where xx is the number of the data set (starting from 11). Then, for each query in order, print the page with the highest score on its own line. If several pages share the highest score, print all of them on one line, separated by single spaces, in increasing order of page number.

Examples1

  1. Example 1

    Input
    2
    3 3
    usc
    great
    sucks
    2
    i am a student at [usc,2] a great school
    i also think that [ucla,3] sucks
    3
    we are usc the university of southern california
    we are located in the same town as [ucla,3]
    we have many excellent [students,1]
    2
    we are ucla
    we are a great great school
    1 2
    usc
    1
    empty page
    2
    [link,1] usc usc [usc,1] text text text text text
    usc usc usc usc usc usc usc usc usc usc usc usc
    
    Expected output
    Data Set 1:
    2
    2 3
    3
    Data Set 2:
    1 2