题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
与HJ70题类似的利用栈的概念,专门有一个list记录前引号在栈中出现的位置,方便一次性弹出引号内部字符并组装,再放入同样的位置。
s = input().split()[::-1]
stack = []
pos = []
while len(s) > 0:
ele = s.pop()
if ele[0] == '"':
pos.append(len(stack))
if ele[-1]=='"':
stack.append(ele[1:-1])
else:
stack.append(ele[1:])
#print('pos',pos)
#print('front',stack)
elif ele[-1] == '"':
front = pos.pop()
temp = [ele[:-1]]
while len(stack) > front:
temp.append(stack.pop())
## 考虑到 “ab ab”的顺序,此处temp不能str形式直接用+=
## 否则翻转顺序时会颠倒ab-->ba
#print('temp',temp)
stack.append(' '.join(map(str, temp[::-1])))
## 引号间的多个字符串中间是以空格隔开,现在将
## 建立s时去掉的空格补回来
#print('end',stack)
else:
stack.append(ele)
#print('+',stack)
print(len(stack))
for i in stack:
print(i)
""" 需要注意的测试用例
u "a e i o u" r
ab "ab ab"
"""

