Computer Science
InterviewTime limit1sMemory limit128 MB
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 is relevant either because (1) the query word appears in , or because (2) the query word appears in a page that links to , close to that hyperlink.
Scoring. Let be the query word.
- earns one point for every occurrence of in .
- For every hyperlink that goes from some page to , and for every occurrence of in at word distance from , page earns points — that is, points when , and points otherwise.
The displayed word of a hyperlink is itself a word of the document: it counts as an occurrence when it equals , and it occupies its own position when distances are measured. In particular, if is the displayed word of the hyperlink itself, that occurrence is at distance and is worth points. Distances are counted in words over the whole document, ignoring line breaks. The same occurrence in may be counted several times if it is close to several hyperlinks pointing to .
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 , the number of data sets. It is followed by data sets of the following form.
The first line of a data set contains two integers and : the number of queries and the number of web pages. Both are between and .
The next lines each contain one query: a single word of at most lowercase letters.
Then come the descriptions of the web pages. The description of page starts with a line containing the number of lines that page consists of (between and ), followed by lines of text of at most 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 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 . The target is always a valid page between and , 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 is the number of the data set (starting from ). 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.