largest possible rectangle of letters

后端 未结 3 990
故里飘歌
故里飘歌 2021-02-01 10:04

Write a program to find the largest possible rectangle of letters such that every row forms a word (left to right) and every column forms a word (top to bottom).

3条回答
  •  别那么骄傲
    2021-02-01 10:06

    Given the dictionary of words of a given length, create two new dictionaries: The first contains all single letter prefixes of words (all letters that can be the first letter of a word of the given length), and the second contains all double letter prefixes of words (all sequences of two letters that can be the first two letters of a word of the given length). You can do triple prefixes as well, but you probably won't need to go beyond that.

    1. Choose a word from the dictionary, call it X. This will be the first row of the matrix.

    2. Check that X[1], X[2], ..., X[N] are all valid single letter prefixes using that handy list you made. If they are, go on to step 3; otherwise go back to step 1.

    3. Choose a word from the dictionary, call it Y. This will be the second row of the matrix.

    4. Check that X[1] Y[1], X[2] Y[2], ..., X[N] Y[N] are all valid double letter prefixes using that handy list you made. If they are, go on to step 5; otherwise go back to step 3. If this was the last word in the dictionary, then go all the way back to step 1.

      ...

      2(N-1). Choose a word from the dictionary, call it Z. This will be the Nth row of the matrix.

      2N. Check that X[1] Y[1] ... Z[1], X[2] Y[2] ... Z[2], ..., X[N] Y[N] ... Z[N] are all words in the dictionary. If they are, congrats, you've done it! Otherwise go back to step 2(N-1). If this was the last word in the dictionary, then go all the way back to step 2(n-3).

    The logic is to build up the rectangle of words one row at a time, selecting words for the rows and then checking that the column could be completed to words. This will progress much faster than adding one letter at a time.

提交回复
热议问题