#P9184. Maximum Moo Sentences

    ID: 22339 Type: Default 1000ms 256MiB

Maximum Moo Sentences

Maximum Moo Sentences

Farmer John speaks the Moo language, which is a minimalistic variant of English. In Moo, there are only four types of words: nouns, transitive verbs, intransitive verbs, and conjunctions. There are also only two types of punctuation marks: periods and commas. Every two consecutive words are separated by a space. When a period or comma appears after a word, it is attached to that word and, if another word follows, a space is inserted after the punctuation.

A sentence in Moo must follow one of the following formats:

  • Type 1: noun + intransitive verb.
  • Type 2: noun + transitive verb + noun(s). In Type 2, at least one noun must follow the transitive verb, and every noun following the first must be preceded immediately by a comma (which is then followed by a space if another word follows).

Additionally, two sentences may be joined into a compound sentence if a conjunction is placed between them. Note that:

  • The resulting compound sentence cannot be joined further with any other sentence or compound sentence.
  • Every sentence (or compound sentence) must end with a period (the period is attached directly to the final word).

Farmer John has a word bank consisting of N words, C commas, and P periods. Each word or punctuation mark may be used at most as many times as it appears in the bank. Help Farmer John construct a sequence of valid Moo sentences that uses the maximum possible number of words.

Important details:

  • You may choose between simple sentences (Type 1 or Type 2) or use compound sentences (by joining two simple sentences with a conjunction) in order to get the best "words per period" ratio. For instance, two simple Type 1 sentences would normally use 2 periods and 4 words, but if combined into a compound sentence, they use only 1 period, one extra conjunction word, and 5 words in total.
  • In a Type 2 sentence, if extra nouns are added beyond the first required noun, every extra noun (after the first) must be preceded by a comma, which counts against the available commas.
  • Strategically, you can use compound sentences to save on period usage, and wisely distribute extra nouns with the available commas. In a sentence that is converted from the Type 1 formation (noun + intransitive verb) into a Type 2 formation (noun + transitive verb + extra noun(s)), the extra noun added as the first noun does not require a comma, but any additional extra nouns do.

The input consists of T independent instances. For each instance, you are given three integers N, C, and P. You must output a valid Moo language text (i.e. a sequence of sentences) that uses as many words as possible, without exceeding the available number of words or punctuation marks.

The following outlines a recommended strategy:

  1. Form as many compound sentences as possible. A compound sentence is made of two simple sentence parts. A basic Type 1 sentence (i.e. a b) uses 2 words; a compound sentence (joining two Type 1 sentences with the conjunction d) uses 5 words (a b d a b) and only 1 period instead of 2.
  2. If extra words remain, you can form additional simple sentences (each using 2 words) using available periods.
  3. After establishing the base sentences (with a total word cost computed as 5 × (number of compound sentences) plus 2 × (number of simple sentences)), you may append extra nouns to these sentences. Each extra noun increases the word count by 1. However, for each sentence, the first extra noun can be added without cost. Any extra noun beyond the first in the same sentence requires a comma, so distribute extra nouns as evenly as possible to avoid using extra commas.
  4. The maximum extras you can add across all sentence units is (total number of sentence units) + C since every sentence can get one free extra and then each additional extra in a sentence consumes one comma. Your goal is to add as many extra nouns as allowed by both the remaining word count and the punctuation constraints.
  5. </p>

    Your program should construct and output one valid Moo text per test case that meets the above requirements.

    inputFormat

    The first line of input contains a single integer T indicating the number of test cases. Each of the following T lines contains three space‐separated integers: N (the total number of words available), C (the total number of commas available), and P (the total number of periods available).

    outputFormat

    For each test case, output a single line containing a Moo language text (i.e. a sequence of sentences) that uses the maximum possible number of words while respecting the grammatical rules and available punctuation. Each sentence must follow one of the valid formats described above. Ensure that periods and commas appear in correct positions as specified.

    sample

    2
    10 1 2
    11 2 2
    a b d a b. a b d a b.
    

    a c a d a b. a b d a b.

    </p>