题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677?tpId=37&tqId=21297&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
import sys
'''
解析字符串接收
'''
s =input()
s = list(s)
def ParameterResolution(s):
'''
定义解析函数
整体的思路是用*号替换" "中的空格,用index来标记""外空格的位置,
不停的移动index,寻找字符串中包含"",并""中有空格的字符串,将Index添加到列表L2中,还原的时候用index找到经过split分割的字符串,把替换的空格还原。
可能存在多组"",所以需要添加到列表中。
'''
#index最初开始指向的位置为第一个元素前面,假设该元素前面有个空格,
index=0
#L2存储后面有"其中有空格",的空格
L2=[]
i=0
while i< (len(s)):
#发现一个空格,index+1
if s[i]== " ":
index += 1
i+=1
#找""的开始值和结束值
elif s[i]=='"':
count=0
#开始值"
xstart=i
i+=1
while i<len(s)and s[i] !='"' :
i+=1
#print(s[i])
#发现第二个后停止检索,将i的值付给xend
xend = i
i+=1
#围了方便继续使用i,需要将i+1准备进行下一次循环
for j in range(len(s[xstart:xend+1])):
if s[xstart:xend+1][j]==" ":
s[xstart+j]="*"
#利用countJinxing统计,如果count为0,说明没有空格,那么L2中不添加这个index
count+=1
if count!=0:
L2.append(index)
else:
i+=1
L1 = "".join(s).split()
#还原,因为L2是后面有"",并中间有空格的index,所以我们根据index遍历L1中的经过空格分割的列表
for x in L2:
L1[x]=L1[x].replace("*"," ")
print(len(L1))
for x in L1:
print(x.strip('"'))
ParameterResolution(s)
