首页 > 爱奇艺算法笔试
头像
牛客0815号
编辑于 2020-08-23 21:38
+ 关注

爱奇艺算法笔试

第一题分词ac了,求问第二题格式化咋做
import sys
import re
def my_split(line, words):
pattern1 = re.compile('(' + '|'.join(words) + ')')
array1 = re.split(pattern1, line)
return array1
def segment(sent, words):
line = sent.replace(' ','')
sent_list = sent.split()
pattern1 = re.compile( '(' +'|'.join(words) + ')')
array1 = re.split(pattern1, line)
pattern2 = re.compile('|'.join(words))
array2 = re.split(pattern2, line)
ret = []
i = 0
j = 1
while i < len(array2):
ret.extend(my_split(array2[i],sent_list))
if j < len(array1):
ret.append(array1[j])
i += 1
j += 2
for i in ret:
if i:
print(i, end=' ')
str1 = sys.stdin.readline().strip()
array = sys.stdin.readline().strip().split()
segment(str1,array)

全部评论

(1) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐