s = raw_input()
level = 0
i = 0
while i < len(s):
l = s.find('<',i)
r = s.find('>',l+1)
if l >= 0 and r >= 0:
if l != i:
print(' '*4*level+s[i:l])
t = s[l:r+1]
if t[-2] == '/':
print(' '*4*level+t)
elif t[1] == '/':
level -= 1
print(' '*4*level+t)
else :
print(' '*4*level+t)
level += 1
i = r + 1
else :
print(' '*4*level+s[i:])
break
Question_ID: Q001
Language: Python
Time Cost: 40-mins
Time Complexity: O(n)
Solution
主要思路是查找
<>
标签对内容划分层级,按格式输出<>
标签,输出标签前的内容,没找到直接输出<a/>
直接输出标签<a>
则输出标签,之后层级加 1</a>
则层级减 1,输出标签My Code
Other
请多多指教