21076_Wordladder

2022-5-16 18:18| 发布者: Hocassian| 查看: 49| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-3-89504808932100-Problem List-采集的数据-后羿采集器.html

Pro.ID

21076

Title

Word ladder

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21076

AC

9

Submit

34

Ratio

26.47%

时间&空间限制

  • Time Limit: 9000/3000 MS (Java/Others)     Memory Limit: 131072/65536 K (Java/Others)
  • 描述

    You now work for a puzzle company. They have a puzzle they call a Word Ladder. A solver starts with a given starting word, and makes changes one letter at a time until s/he reaches a target word, with no word in the chain appearing more than once. There are three ways to take a single step from one word to another:
    • Change one letter
    • Add one letter
    • Remove one letter
    So, it’s one step from COT to CAT, one step from CAT to SCAT, and one step from SCAT to SAT.
    Here’s a word ladder from COT to SCAT:
    COT => CAT => SAT => SCAT
    Here’s another word ladder from COT to SCAT:
    COT  =>  CAT  =>  SCAT
    The length of a word ladder is the number of words in it, so the examples above show a word ladder of length 4, and one of length 3. The second is the shortest possible between COT and SCAT. Shorter ladders are considered better than longer ladders.
    The puzzle company knows that, given two words, a smart solver will always find the best ladder, which is the shortest ladder, between them. They want to give their solvers a challenge, so they are looking for long word ladders. Given a limited vocabulary, you need to tell them the length of the longest word ladder that a smart solver would find using only words in that vocabulary - that is, the longest of all best ladders.

    输入

    Input will consist of multiple datasets. Each dataset starts with an integer N (1 <= N <= 500) which indicates the number of words in the vocabulary. The words follow, one per line.
    Each word will consist only of 1 to 50 lower-case letters. There will be no other characters or white space.
    The end of input is indicated by a line containing a single zero.

    输出

    Description
    You now work for a puzzle company. They have a puzzle they call a Word Ladder. A solver starts with a given starting word, and makes changes one letter at a time until s/he reaches a target word, with no word in the chain appearing more than once. There are three ways to take a single step from one word to another:
    • Change one letter
    • Add one letter
    • Remove one letter
    So, it’s one step from COT to CAT, one step from CAT to SCAT, and one step from SCAT to SAT.
    Here’s a word ladder from COT to SCAT:
    COT => CAT => SAT => SCAT
    Here’s another word ladder from COT to SCAT:
    COT  =>  CAT  =>  SCAT
    The length of a word ladder is the number of words in it, so the examples above show a word ladder of length 4, and one of length 3. The second is the shortest possible between COT and SCAT. Shorter ladders are considered better than longer ladders.
    The puzzle company knows that, given two words, a smart solver will always find the best ladder, which is the shortest ladder, between them. They want to give their solvers a challenge, so they are looking for long word ladders. Given a limited vocabulary, you need to tell them the length of the longest word ladder that a smart solver would find using only words in that vocabulary - that is, the longest of all best ladders.
    Input
    Input will consist of multiple datasets. Each dataset starts with an integer N (1 <= N <= 500) which indicates the number of words in the vocabulary. The words follow, one per line.
    Each word will consist only of 1 to 50 lower-case letters. There will be no other characters or white space.
    The end of input is indicated by a line containing a single zero.
    Output
    For each input set, print a line containing a single integer representing the length of the longest ladder that a smart solver would find.
    Sample Input
    4
    cat
    cot
    scat
    sat
    7
    welcome
    to
    the
    acm
    regional
    programming
    contest
    0
    Sample Output
    3
    1
    Source

    样例输入

    4
    cat
    cot
    scat
    sat
    7
    welcome
    to
    the
    acm
    regional
    programming
    contest
    0

    样例输出

    3
    1

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部