从前往后扫描一遍,遇到 < 就匹配tag,start tag 缩进加4,end tag 缩进减4,其余(empty-element tag 和 content)缩进保持。
My Code
main = do
s <- getLine
putStr $ format s
format s = helper s ""
helper "" i = ""
helper ('<':'/':t) i = etag t i "/<"
helper ('<':t) i = ztag t i "<"
helper t i = ctag t i ""
etag ('>':t) i c = j ++ reverse c ++ ">\n" ++ helper t j
where j = drop 4 i
etag (h:t) i c = etag t i (h:c)
ztag ('/':'>':t) i c = i ++ reverse c ++ "/>\n" ++ helper t i
ztag ('>':t) i c = i ++ reverse c ++ ">\n" ++ helper t j
where j = " " ++ i
ztag (h:t) i c = ztag t i (h:c)
ctag t@('<':_) i c = i ++ reverse c ++ "\n" ++ helper t i
ctag (h:t) i c = ctag t i (h:c)
Question_ID: Q001
Language: Haskell
Time Cost: 40-mins
Time Complexity: O(n)
Solution
从前往后扫描一遍,遇到
<
就匹配tag,start tag 缩进加4,end tag 缩进减4,其余(empty-element tag 和 content)缩进保持。My Code
Other
感觉写这类题用支持模式匹配的语言比较爽。