挑战的目标是编写一个程序,该程序以树的形式可视化依赖图。尽管在这种情况下“依赖关系图”仅意味着有向图,但此处描述的可视化方法最适合描述某些依赖关系的图(作为练习,在阅读了挑战之后,尝试反转其中一个的方向示例图,看看结果是否同样有用。)
程序的输入由一个或多个目标定义组成,这些目标定义是以下形式的行:
Target DirectDependency1 DirectDependency2 ...
,定义目标及其相关的直接依赖项(如果有)。目标及其依赖关系统称为对象。如果对象仅显示为依赖项,而不显示为目标,则它没有依赖项。输入中出现的所有对象的集合称为Γ。(有关输入格式的更多详细信息,请参见“输入和输出”部分。)
对于任意一对对象A和B,我们说:
- 对于某些对象 B',如果 A直接依赖于 B,或者 A直接依赖于 B',而 B'依赖于 B,则 A依赖于B(等效地, B是A所必需的);
- 如果 A依赖于 B,而 B不依赖于 A,则 A适当地依赖于B(等效地, A适当地要求B)。
我们在Γ中定义了一个人为对象oo such,使得ʀooᴛ并不是任何对象都直接需要的,并且对于所有对象A,andooᴛ直接且仅当A在Γ中且A不是时才依赖于A通过在Γ的任意对象正确需要(换句话说,ʀooᴛ直接取决于甲如果没有其他对象取决于甲,或如果依赖于所有对象甲还需要由甲)。
输出树
我们构造一棵树,其根节点为ʀooᴛ,并且每个节点的子节点都是其直接依赖项。例如,给定输入
Bread Dough Yeast
Dough Flour Water
Butter Milk
,结果树是
,或者以ASCII形式,
ʀooᴛ
+-Bread
| +-Dough
| | +-Flour
| | +-Water
| +-Yeast
+-Butter
+-Milk
。程序的输出是上面定义的树,没有 oo节点打印。因此,例如,上述输入的相应输出为
Bread
+-Dough
| +-Flour
| +-Water
+-Yeast
Butter
+-Milk
。稍后给出输出树的布局的详细描述。
节点顺序
给定父节点的子节点P,应该分类,使得对所有的子节点一和乙的P,一出现之前乙当且仅当
- 存在一个子节点Ç的P,使得甲正确由需要Ç,和Ç先于或等于,乙,根据相同的顺序; 或者,
- 甲字母顺序先于乙(更preceisely,甲先于乙使用ASCII整理,)和存在没有子节点Ç的P,以使得乙正确由需要Ç,和Ç先于或等于,甲,根据相同的顺序。
(正在寻找数学挑战的人们可能想要证明这种关系定义明确,并且实际上是严格的总阶。不要忘记Γ是有限的!)
例如,给定输入
X D C B A
B D
C A
,输出应为
X
+-A
+-D
+-B
| +-D
+-C
+-A
。
由于字母顺序,A
出现在之前B
,B
出现在之前C
;
D
出现在之前B
,因为它是正确需要的;出现在之后,因为它是A
按字母顺序跟随的;
B
并且C
不会出现在之前D
,即使它们按字母顺序排在前面,也因为存在一个节点,即,该节点B
正确地需要D
,并且等于B
(即,本身),并且C
根据相同的规则在之前。
重复次数
例如,如果多个对象需要同一个对象A,则在输出中可能会出现多次。如果A没有自己的依赖性,则在这种情况下不需要特殊处理。否则,为了最大程度地减少输出的冗长性,并避免由于循环依赖性而导致的无限递归,仅在第一次出现时列出A的依赖性,而其祖先都不是另一个A节点的兄弟。如A所示,其他任何出现的A都应没有子代,并且应出现一个空格和一个省略号。A
...
例如,给定输入
IP Ethernet
TCP IP
UDP IP
WebRTC TCP UDP
,输出应为
WebRTC
+-TCP
| +-IP
| +-Ethernet
+-UDP
+-IP ...
。再举一个例子,兼具循环依赖和祖先考虑,
Rock Scissors
Paper Rock
Scissors Paper
,应导致
Paper
+-Rock ...
Rock
+-Scissors ...
Scissors
+-Paper ...
。请注意,例如,第一次出现Rock
不会列出其依赖项,因为其父项Paper
是另一个Rock
节点的同级项。第二个Rock
节点的父级ʀooᴛ(未出现在输出中)没有Rock
作为同级,因此的依赖项Rock
在此节点上列出。
输出树布局
我确定您掌握了如何将树表示为ASCII艺术(如果可以的话,可以随时跳过此部分),但出于完整性考虑...
ʀooᴛ的子节点按顺序打印在单独的行上,没有任何缩进。每个节点后面紧跟着其子节点(如果有的话),以相同的方式递归打印,并在右边缩进两个字符。对于每个有子节点的节点,由|
(竖线)字符组成的垂直线从该节点的第一个字符正下方的字符向下延伸,直到其最后一个子节点的行,不包括最后一个子节点的子节点。如果节点的缩进不为零,则+-
在其缩进之前(在与父节点相同的缩进级别),覆盖上述垂直线。
输入输出
您可以通过STDIN或等效方法读取输入。您可以假定没有空行,并且您可能要求最后一行以换行符结束或不结束。您可以假定对象名称由可打印的ASCII字符(不包括空格)组成。您可以假定目标定义中的对象由单个空格字符分隔,并且没有前导或尾随空格。您可以假定每个目标最多定义一次,并且其依赖项列表中没有重复项。
您可以将输出写入STDOUT,或使用等效方法。除最长的输出线外,所有输出线都可以包含尾随空格。最后的输出行可以或可以不以换行符结尾。
得分
这是代码高尔夫球。在简短的回答,以字节为单位,胜。
测试用例
您的程序应在合理的时间内处理以下每个测试用例。
输入项
Depender Dependee
Independent
输出量
Depender
+-Dependee
Independent
输入项
Earth Turtle
Turtle Turtle
输出量
Earth
+-Turtle
+-Turtle ...
输入项
F A C B D I
A B
B A C
D E H
C
G F
J H G C E I
E D
H D
I G
输出量
J
+-C
+-E
| +-D
| +-E ...
| +-H ...
+-H
| +-D ...
+-G
| +-F
| +-C
| +-A
| | +-B ...
| +-B
| | +-C
| | +-A ...
| +-D ...
| +-I ...
+-I
+-G ...
文明V技术树
输入项
Pottery Agriculture
AnimalHusbandry Agriculture
Archery Agriculture
Mining Agriculture
Sailing Pottery
Calendar Pottery
Writing Pottery
Trapping AnimalHusbandry
TheWheel AnimalHusbandry
Masonry Mining
BronzeWorking Mining
Optics Sailing
Philosophy Writing
HorsebackRiding TheWheel
Mathematics TheWheel Archery
Construction Masonry
IronWorking BronzeWorking
Theology Calendar Philosophy
CivilService Philosophy Trapping
Currency Mathematics
Engineering Mathematics Construction
MetalCasting Construction IronWorking
Compass Optics
Education Theology CivilService
Chivalry CivilService HorsebackRiding Currency
Machinery Engineering
Physics Engineering MetalCasting
Steel MetalCasting
Astronomy Compass Education
Acoustics Education Chivalry
Banking Chivalry
PrintingPress Machinery Phyisics
Gunpowder Physics Steel
Navigation Astronomy
Economics Banking PrintingPress
Chemistry Gunpowder
Metallurgy Gunpowder
Archaeology Navigation
ScientificTheory Navigation Acoustics Economics
MilitaryScience Economics Chemistry
Fertilizer Chemistry
Rifling Metallurgy
Biology Archaeology ScientificTheory
SteamPower ScientificTheory MilitaryScience
Dynamite MilitaryScience Fertilizer Rifling
Electricity Biology SteamPower
ReplaceableParts SteamPower
Railroad SteamPower Dynamite
Refrigeration Electricity
Telegraph Electricity
Radio Electricity
Flight ReplaceableParts
Combustion ReplaceableParts Railroad
Penicillin Refrigeration
Plastics Refrigeration
Electronics Telegraph
MassMedia Radio
Radar Radio Flight Combustion
AtomicTheory Combustion
Ecology Penicillin Plastics
Computers Electronics MassMedia Radar
Rocketry Radar
Lasers Radar
NuclearFission AtomicTheory
Globalization Ecology Computers
Robotics Computers
Satellites Rocketry
Stealth Lasers
AdvancedBallistics Lasers NuclearFission
ParticlePhysics Robotics Satellites
NuclearFusion Satellites Stealth AdvancedBallistics
Nanotechnology ParticlePhysics
FutureTech Globalization Nanotechnology NuclearFusion
输出量
FutureTech
+-Globalization
| +-Computers
| | +-Electronics
| | | +-Telegraph
| | | +-Electricity
| | | +-Biology
| | | | +-Archaeology
| | | | | +-Navigation
| | | | | +-Astronomy
| | | | | +-Compass
| | | | | | +-Optics
| | | | | | +-Sailing
| | | | | | +-Pottery
| | | | | | +-Agriculture
| | | | | +-Education
| | | | | +-CivilService
| | | | | | +-Philosophy
| | | | | | | +-Writing
| | | | | | | +-Pottery ...
| | | | | | +-Trapping
| | | | | | +-AnimalHusbandry
| | | | | | +-Agriculture
| | | | | +-Theology
| | | | | +-Calendar
| | | | | | +-Pottery ...
| | | | | +-Philosophy ...
| | | | +-ScientificTheory
| | | | +-Acoustics
| | | | | +-Chivalry
| | | | | | +-CivilService ...
| | | | | | +-Currency
| | | | | | | +-Mathematics
| | | | | | | +-Archery
| | | | | | | | +-Agriculture
| | | | | | | +-TheWheel
| | | | | | | +-AnimalHusbandry ...
| | | | | | +-HorsebackRiding
| | | | | | +-TheWheel ...
| | | | | +-Education ...
| | | | +-Economics
| | | | | +-Banking
| | | | | | +-Chivalry ...
| | | | | +-PrintingPress
| | | | | +-Machinery
| | | | | | +-Engineering
| | | | | | +-Construction
| | | | | | | +-Masonry
| | | | | | | +-Mining
| | | | | | | +-Agriculture
| | | | | | +-Mathematics ...
| | | | | +-Phyisics
| | | | +-Navigation ...
| | | +-SteamPower
| | | +-MilitaryScience
| | | | +-Chemistry
| | | | | +-Gunpowder
| | | | | +-Physics
| | | | | | +-Engineering ...
| | | | | | +-MetalCasting
| | | | | | +-Construction ...
| | | | | | +-IronWorking
| | | | | | +-BronzeWorking
| | | | | | +-Mining ...
| | | | | +-Steel
| | | | | +-MetalCasting ...
| | | | +-Economics ...
| | | +-ScientificTheory ...
| | +-MassMedia
| | | +-Radio
| | | +-Electricity ...
| | +-Radar
| | +-Combustion
| | | +-Railroad
| | | | +-Dynamite
| | | | | +-Fertilizer
| | | | | | +-Chemistry ...
| | | | | +-MilitaryScience ...
| | | | | +-Rifling
| | | | | +-Metallurgy
| | | | | +-Gunpowder ...
| | | | +-SteamPower ...
| | | +-ReplaceableParts
| | | +-SteamPower ...
| | +-Flight
| | | +-ReplaceableParts ...
| | +-Radio ...
| +-Ecology
| +-Penicillin
| | +-Refrigeration
| | +-Electricity ...
| +-Plastics
| +-Refrigeration ...
+-Nanotechnology
| +-ParticlePhysics
| +-Robotics
| | +-Computers ...
| +-Satellites
| +-Rocketry
| +-Radar ...
+-NuclearFusion
+-AdvancedBallistics
| +-Lasers
| | +-Radar ...
| +-NuclearFission
| +-AtomicTheory
| +-Combustion ...
+-Satellites ...
+-Stealth
+-Lasers ...
Cygwin syslog-ng软件包依赖关系图
输入项
p11-kit-trust cygwin libtasn1_6
libtasn1_6 cygwin
libp11-kit0 cygwin libffi6 libintl8
libopenssl100 cygwin libgcc1 zlib0 ca-certificates
gsettings-desktop-schemas libglib2.0_0
p11-kit cygwin libp11-kit0 libtasn1_6
zlib0 libgcc1
libpcre1 cygwin
libfam0 gamin
shared-mime-info cygwin libglib2.0_0 libxml2
coreutils cygwin libattr1 libgcc1 libgmp10 libiconv2 libintl8 tzcode _update-info-dir
syslog-ng bash gawk libevtlog0 libgcc1 libglib2.0_0 libopenssl100 libpcre1 libwrap0 tzcode
libxml2 libiconv2 libreadline7 zlib0
cygwin base-cygwin
ca-certificates bash p11-kit p11-kit-trust cygwin
libintl8 cygwin libiconv2
libncursesw10 cygwin libgcc1 libstdc++6 terminfo
libstdc++6 libgcc1
bash coreutils cygwin libgcc1 libiconv2 libintl8 libncursesw10 libreadline7 _update-info-dir
libmpfr4 libgcc1 libgmp10
tzcode bash cygwin libgcc1
_update-info-dir cygwin
libreadline7 libncursesw10
gawk bash cygwin libgcc1 libgmp10 libintl8 libmpfr4 libreadline7
gamin libglib2.0_0
libglib2.0_0 libfam0 libffi6 libgcc1 libiconv2 libintl8 libpcre1 zlib0 gsettings-desktop-schemas shared-mime-info tzcode
输出量
syslog-ng
+-libgcc1
+-bash
| +-cygwin
| | +-base-cygwin
| +-_update-info-dir
| | +-cygwin ...
| +-libgcc1
| +-libiconv2
| +-libintl8
| | +-cygwin ...
| | +-libiconv2
| +-libncursesw10
| | +-cygwin ...
| | +-libgcc1
| | +-libstdc++6
| | | +-libgcc1
| | +-terminfo
| +-libreadline7
| | +-libncursesw10 ...
| +-coreutils
| +-cygwin ...
| +-_update-info-dir ...
| +-libattr1
| +-libgcc1
| +-libgmp10
| +-libiconv2
| +-libintl8 ...
| +-tzcode ...
+-tzcode
| +-cygwin ...
| +-libgcc1
| +-bash ...
+-gawk
| +-cygwin ...
| +-libgcc1
| +-libgmp10
| +-libintl8 ...
| +-libreadline7 ...
| +-bash ...
| +-libmpfr4
| +-libgcc1
| +-libgmp10
+-libevtlog0
+-libpcre1
| +-cygwin ...
+-libglib2.0_0
| +-libffi6
| +-libgcc1
| +-libiconv2
| +-libintl8 ...
| +-libpcre1 ...
| +-tzcode ...
| +-zlib0
| | +-libgcc1
| +-gsettings-desktop-schemas
| | +-libglib2.0_0 ...
| +-libfam0
| | +-gamin
| | +-libglib2.0_0 ...
| +-shared-mime-info
| +-cygwin ...
| +-libxml2
| | +-libiconv2
| | +-libreadline7 ...
| | +-zlib0 ...
| +-libglib2.0_0 ...
+-libopenssl100
| +-cygwin ...
| +-libgcc1
| +-ca-certificates
| | +-cygwin ...
| | +-bash ...
| | +-p11-kit
| | | +-cygwin ...
| | | +-libp11-kit0
| | | | +-cygwin ...
| | | | +-libffi6
| | | | +-libintl8 ...
| | | +-libtasn1_6
| | | +-cygwin ...
| | +-p11-kit-trust
| | +-cygwin ...
| | +-libtasn1_6 ...
| +-zlib0 ...
+-libwrap0
GNU grep regex.c
调用图
输入项
check_dst_limits_calc_pos_1 check_dst_limits_calc_pos_1
re_string_destruct
match_ctx_add_sublast
build_charclass bitset_set
match_ctx_init
rpl_re_set_syntax
link_nfa_nodes re_node_set_init_1 re_node_set_init_2
re_node_set_init_union re_node_set_init_copy
register_state re_node_set_insert_last re_node_set_alloc
rpl_re_search re_search_stub
re_node_set_init_2
re_search_2_stub re_search_stub
merge_state_array re_acquire_state re_node_set_init_union
calc_eclosure calc_eclosure_iter
create_token_tree
create_tree create_token_tree
pop_fail_stack
re_compile_fastmap_iter
rpl_regcomp rpl_re_compile_fastmap re_compile_internal
check_arrival_expand_ecl find_subexp_node check_arrival_expand_ecl_sub re_node_set_merge re_node_set_alloc
clean_state_log_if_needed extend_buffers
lower_subexps lower_subexp
duplicate_node re_dfa_add_node
re_node_set_merge
build_equiv_class bitset_set
create_initial_state re_acquire_state_context re_node_set_merge re_node_set_init_copy re_node_set_contains
rpl_regfree free_dfa_content
check_halt_state_context check_halt_node_context re_string_context_at
check_dst_limits search_cur_bkref_entry check_dst_limits_calc_pos
re_node_set_insert re_node_set_init_1
transit_state_bkref transit_state_bkref check_subexp_matching_top get_subexp re_string_context_at re_node_set_init_union re_acquire_state_context
get_subexp_sub match_ctx_add_entry clean_state_log_if_needed check_arrival
free_tree free_token
parse_reg_exp free_tree parse_branch fetch_token postorder create_tree
re_string_skip_chars
match_ctx_clean
re_copy_regs
parse_expression parse_dup_op parse_sub_exp build_charclass_op parse_expression init_word_char create_tree free_tree create_token_tree fetch_token parse_bracket_exp postorder
init_dfa
re_acquire_state_context calc_state_hash re_node_set_compare create_cd_newstate
re_node_set_add_intersect
merge_state_with_log transit_state_bkref check_subexp_matching_top re_acquire_state_context re_string_context_at re_node_set_init_union
free_fail_stack_return
check_subexp_limits sub_epsilon_src_nodes re_node_set_contains
build_charclass_op create_tree bitset_not bitset_set build_charclass create_token_tree bitset_mask free_charset
re_node_set_alloc
check_arrival_add_next_nodes check_node_accept_bytes re_acquire_state re_node_set_merge check_node_accept re_node_set_insert
check_node_accept_bytes re_string_char_size_at re_string_wchar_at re_string_elem_size_at
re_string_allocate re_string_construct_common re_string_realloc_buffers
lower_subexp create_tree
parse_branch parse_expression create_tree postorder free_tree
rpl_regexec re_search_internal
free_state
expand_bkref_cache re_node_set_init_1 re_node_set_insert search_cur_bkref_entry re_node_set_merge re_node_set_init_copy check_arrival_expand_ecl re_node_set_contains re_acquire_state
peek_token re_string_peek_byte_case re_string_wchar_at peek_token
re_string_construct build_wcs_upper_buffer re_string_construct_common build_upper_buffer re_string_translate_buffer re_string_realloc_buffers build_wcs_buffer
re_string_realloc_buffers
calc_inveclosure re_node_set_insert_last
sub_epsilon_src_nodes re_node_set_add_intersect re_node_set_contains re_node_set_remove_at
sift_ctx_init
re_string_fetch_byte_case re_string_char_size_at
find_recover_state merge_state_with_log
sift_states_iter_mb check_node_accept_bytes re_node_set_contains
group_nodes_into_DFAstates bitset_set_all bitset_copy bitset_set re_node_set_init_1 re_node_set_insert re_node_set_init_copy bitset_merge bitset_clear bitset_contain bitset_empty
push_fail_stack re_node_set_init_copy
check_node_accept bitset_contain re_string_context_at
match_ctx_free match_ctx_clean
build_wcs_upper_buffer
rpl_regerror
sift_states_backward re_node_set_init_1 update_cur_sifted_state build_sifted_states
build_sifted_states re_node_set_insert sift_states_iter_mb check_dst_limits check_node_accept re_node_set_contains
transit_state_mb check_node_accept_bytes re_acquire_state_context re_string_context_at re_node_set_init_union clean_state_log_if_needed
optimize_utf8
build_collating_symbol bitset_set
re_search_stub rpl_re_compile_fastmap re_search_internal re_copy_regs
check_matching merge_state_with_log find_recover_state re_acquire_state_context transit_state check_subexp_matching_top transit_state_bkref extend_buffers check_halt_state_context re_string_context_at
extend_buffers build_wcs_upper_buffer re_string_translate_buffer re_string_realloc_buffers build_wcs_buffer build_upper_buffer
check_arrival re_node_set_init_1 re_string_context_at expand_bkref_cache re_node_set_merge re_node_set_init_copy re_node_set_contains check_arrival_expand_ecl check_arrival_add_next_nodes re_acquire_state_context
free_workarea_compile
match_ctx_add_entry
parse_bracket_symbol re_string_fetch_byte_case
free_charset
build_trtable re_node_set_merge re_node_set_alloc group_nodes_into_DFAstates re_acquire_state_context bitset_merge bitset_contain bitset_empty
re_node_set_init_copy
duplicate_node_closure duplicate_node_closure re_node_set_insert duplicate_node search_duplicated_node
parse_bracket_exp build_equiv_class bitset_not bitset_set parse_bracket_element build_range_exp free_charset peek_token_bracket bitset_mask build_charclass create_tree build_collating_symbol create_token_tree
parse_bracket_element peek_token_bracket re_string_wchar_at parse_bracket_symbol re_string_char_size_at
re_search_internal re_string_destruct re_string_allocate match_ctx_init match_ctx_free check_matching check_halt_state_context re_string_reconstruct match_ctx_clean set_regs prune_impossible_nodes
set_regs free_fail_stack_return update_regs pop_fail_stack proceed_next_node
prune_impossible_nodes sift_ctx_init check_halt_state_context sift_states_backward merge_state_array
check_arrival_expand_ecl_sub re_node_set_insert re_node_set_contains check_arrival_expand_ecl_sub
re_acquire_state re_node_set_compare create_ci_newstate calc_state_hash
build_wcs_buffer
rpl_re_compile_pattern re_compile_internal
free_dfa_content free_token free_state
get_subexp match_ctx_add_sublast get_subexp_sub clean_state_log_if_needed extend_buffers search_cur_bkref_entry find_subexp_node check_arrival
analyze calc_first optimize_subexps postorder lower_subexps calc_eclosure preorder link_nfa_nodes calc_inveclosure calc_next
re_node_set_init_1
duplicate_tree create_token_tree
calc_eclosure_iter duplicate_node_closure calc_eclosure_iter re_node_set_alloc re_node_set_insert re_node_set_merge
bitset_empty
free_token free_charset
bitset_copy
parse_dup_op fetch_token mark_opt_subexp fetch_number create_tree postorder duplicate_tree free_tree
rpl_re_compile_fastmap re_compile_fastmap_iter
re_dfa_add_node
re_node_set_insert_last
sift_states_bkref re_node_set_remove_at sift_states_backward search_cur_bkref_entry re_node_set_insert check_dst_limits merge_state_array re_node_set_init_copy re_node_set_contains
fetch_number fetch_token
match_ctx_add_subtop
fetch_token peek_token
bitset_set_all
re_string_reconstruct build_wcs_buffer re_string_translate_buffer re_string_skip_chars bitset_contain build_upper_buffer build_wcs_upper_buffer re_string_context_at
rpl_re_match_2 re_search_2_stub
create_cd_newstate re_node_set_init_copy re_node_set_remove_at free_state register_state
check_subexp_matching_top match_ctx_add_subtop
proceed_next_node check_node_accept re_node_set_contains push_fail_stack check_node_accept_bytes re_node_set_insert
rpl_re_match re_search_stub
build_range_exp bitset_set
add_epsilon_src_nodes re_node_set_add_intersect re_node_set_merge re_acquire_state re_node_set_alloc
update_regs
rpl_re_search_2 re_search_2_stub
transit_state re_string_context_at build_trtable transit_state_mb
check_dst_limits_calc_pos check_dst_limits_calc_pos_1
build_upper_buffer
calc_first re_dfa_add_node
re_compile_internal optimize_utf8 re_string_destruct init_dfa free_workarea_compile parse create_initial_state analyze free_dfa_content re_string_construct
parse_sub_exp postorder create_tree fetch_token free_tree parse_reg_exp
re_string_context_at bitset_contain
init_word_char
create_ci_newstate register_state free_state re_node_set_init_copy
update_cur_sifted_state check_subexp_limits re_acquire_state sift_states_bkref add_epsilon_src_nodes
parse fetch_token create_tree parse_reg_exp
rpl_re_set_syntax
rpl_re_set_registers
rpl_regerror
输出(糟糕!对于SE来说太长了。)