ChatNOI
시간 제한4초메모리 제한1024 MB
단어 문서가 주어질 때, 시작 k개 단어와 m이 주어지면 각 다음 단어의 최소 우도를 최대화하도록 문장을 완성한다.
문제
Mary is fascinated by the power of large language models. With all the recent hype around chat bots and generative AI, she decided to design her own text generation model called ChatNOI (Chat, but Not Overly Intelligent).
The model is trained on a large document consisting of words, where it will learn to recognise patterns in sequences of words. Specifically, for every distinct sequence of consecutive words appearing in the document, the model will keep track of the frequency of words that occur as the next word following this sequence of words.
As an example, if the model is trained with the parameter on the document
row row to the fishing rocks
out in the ocean they go
a cow is sitting and rowing
and the sun rises
and the sun sets
but the cow and the boat are still there
it will learn that row row is followed once by the word to, and the is followed twice by the word sun and once by the word boat, the sun is followed once by the word rises and once by the word sets, and so on. We call the frequency of a word following a particular sequence of words the likelihood of that word following that sequence.
Mary has figured out how she can use a trained model to rank the quality of a given sentence. She looks at every sequence of consecutive words in the sentence, and the word that follows that sequence. She then calculates the likelihood of that word following that sequence, as per the above definition. The minimum likeliness that she encounters out of all consecutive words is the quality of that sentence.
Continuing with the above example, the sentence cow and the sun rises has a quality of , because cow and is followed by the with a likelihood of , and the is followed by sun with a likelihood of , and the sun is followed by rises with a likelihood of , the minimum of which is . Similarly the sentence and the sun has a quality of and the sentence row to the boat has a quality of .
Now that Mary has designed the model and a way to rank the quality of a given sentence, she turns to you for help in using the model to generate sentences. Given the first words in a sentence and a number , Mary asks you to finish the last words of that sentence so that it has the maximum quality possible according to the trained model. She is pretty excited so she may even ask you to do this multiple times.
입력
The input consists of:
-
One line with two integers and , the number of words in the training document and the training parameter as described above.
-
One line with a sequence of words , the training document. Each word consists of to lowercase characters from the English alphabet.
-
One line with an integer , the number of queries to follow.
-
lines, the th of which describes the th query:
- An integer , where , the number of words that should be generated to complete the sentence in the th query, and
- a sequence of words , the initial part of the sentence in the th query. Each word is guaranteed to have appeared in the training document.
Let denote the sum of over all queries . It is guaranteed that is at most .
출력
Output lines, the th line containing the generated words so that the complete sentence for the th query has the maximum quality possible. You may only use words that appear in the training document. If there are multiple possible solutions for a given query then you may output any one of them.