Making ChatGPT
Time limit1sMemory limit1024 MB
Given a corpus of bracketed sentences, each character is followed by its most frequent next character, and the task is to output characters K to K+M-1 of the generated string.
- Level
Medium4 of 10
- Topics
- String, Hash map, Simulation
- Solved
- No attempts yet
Problem
Jiwon is a graduate student in the AI graduate school at GIST, and in principle should be able to build a home ChatGPT-style model without much trouble. But Jiwon is lazy, so instead of a deep learning language model, Jiwon will use a slightly modified n-gram model, a basic NLP technique.
Given a single character, Jiwon's language model predicts the next character by looking at the training corpus. The training corpus consists of several strings. When a character is given, the model searches the training corpus for every occurrence of , then finds the character that appears most often immediately after , and predicts that the next character after is . Note that is possible. If several characters tie for the most frequent, the model chooses the one with the smallest ASCII code. If the character does not appear in the training corpus, the model cannot predict the next character and an error occurs.
The training corpus uses only the character [ marking the start of a sentence, the character ] marking the end of a sentence, the lowercase letters a to z, and the character -, which replaces spaces. Every sentence in the corpus starts with [, ends with ], and has length at least . The character [ appears only at the start of a sentence, and the character ] appears only at the end of a sentence.
To generate a sentence with the model, first input the start character [. When the model predicts the next character , append it to the sentence being generated. Then input into the model to predict the next character , and repeat. If the end character ] appears during this process, stop generating. Otherwise, the prediction keeps going forever. You can prove that as long as the training corpus is not empty, the model never reaches a state where it cannot predict the next character.
Given the training corpus, guess a substring of the string that this model generates, together with Jiwon.
Input
The first line contains three integers , , and , separated by spaces. The training corpus consists of sentences, and the program must output the characters from the -th to the -th of the string generated by the language model trained on this corpus. (, , )
From the second line onward, the lines each contain one sentence of the training corpus as a string. Each line starts with [ and ends with ], and has length at least . The character [ appears only at the start of each line, and the character ] appears only at the end of each line.
The total length of all strings in the training corpus does not exceed .
Output
Output the characters from the -th to the -th of the string generated by the language model trained on the given corpus. When printing the -th character, if the generated string is too short and has no -th character, print . instead.
Hint
The ASCII code of - is 45, the ASCII code of [ is 91, the ASCII code of ] is 93, and the ASCII code of a is 97.