21079_Trie,TrieAgain

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

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

Pro.ID

21079

Title

Trie, Trie Again

Title链接

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

AC

0

Submit

434

Ratio

0.00%

时间&空间限制

  • Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    Spark Plug Searching, Ltd., is looking to enter the market of web-based applications with a new word processor. The team implementing the spell checker have proposed storing the dictionary in a binary-encoded trie, a binary tree structure in which words are located by moving down the tree, one letter at a time, but discarding any letter from which one takes a right branch.

    The end of a word is indicated whenever a letter has no left child or by an @ character. This trie (in the diagram) contains the words a, abbot, abbey, abed, and bed.

    Some team members have objected that the resulting data structure will be too large for practical use. Others have argued that the size can be dramatically reduced by taking advantage of the fact that many words end in common suffixes. The trie can be scanned for common subtrees and the parents of identical subtrees could be altered so that they all point to a single instance of that subtree. Your job is to demonstrate this process. Write a program to find the shared subtree that would produce the maximum reduction in the number of nodes if all instances of that subtree were replaced by a single shared instance.

    输入

    Input consists of one or more lines, each line describing one tree. Each tree is presented, left justified, in preorder form using at least one and up to 200 characters. The nodes of the tree are denoted by a single lowercase letter or '@'. The character '#' indicates that a node does not have a child in the indicated position. Note that with these conventions, the tree described by the preorder traversal will be unique.

    End of input is indicated by a line consisting of the word "END".

    输出

    Description

    Spark Plug Searching, Ltd., is looking to enter the market of web-based applications with a new word processor. The team implementing the spell checker have proposed storing the dictionary in a binary-encoded trie, a binary tree structure in which words are located by moving down the tree, one letter at a time, but discarding any letter from which one takes a right branch.

    The end of a word is indicated whenever a letter has no left child or by an @ character. This trie (in the diagram) contains the words a, abbot, abbey, abed, and bed.

    Some team members have objected that the resulting data structure will be too large for practical use. Others have argued that the size can be dramatically reduced by taking advantage of the fact that many words end in common suffixes. The trie can be scanned for common subtrees and the parents of identical subtrees could be altered so that they all point to a single instance of that subtree. Your job is to demonstrate this process. Write a program to find the shared subtree that would produce the maximum reduction in the number of nodes if all instances of that subtree were replaced by a single shared instance.

    Input

    Input consists of one or more lines, each line describing one tree. Each tree is presented, left justified, in preorder form using at least one and up to 200 characters. The nodes of the tree are denoted by a single lowercase letter or '@'. The character '#' indicates that a node does not have a child in the indicated position. Note that with these conventions, the tree described by the preorder traversal will be unique.

    End of input is indicated by a line consisting of the word "END".

    Output

    For each input tree, print the non-empty subtree (in the same format) that yields the greatest savings if all instances are replaced by a single shared instance. Then, on the same line, print a space and then the number of nodes that would be saved by this replacement. Print no blank lines between outputs.

    If there is a tie among different subtrees that could yield maximal savings, select the smallest tree from among those tied. If there remains more than one candidate, select the one occurring first in a preorder traversal of the original tree.

    Sample Input
    a@#bbot#ey##ed###bed#######
    END
    Sample Output
    ed### 2
    Source

    样例输入

    a@#bbot#ey##ed###bed#######
    END

    样例输出

    ed### 2

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部