第一道分词,思路是栈加暴力匹配,每次将栈顶弹出,然后判断是否能匹配到第一个词,后面将结果重新入栈(AC)
def judge(a,b,word): text=a+b find=False for i in word: if i in text: find=True break if not find:return [a,b] else: idx=text.index(i) before=text[:idx] after=text[idx+len(i):] return [before,i,after] sentence=['aa','bcd','edf','deda'] word=['ded'] stack=[] for i in range(len(sentence)): if i==0:stack.append(sentence[i]) else: cur=stack.pop() ans=judge(cur,sentence[i],word) for j in ans: stack.append(j) print(' '.join(stack))
第二道完全没半点思路,求AC过的大佬分享下。
全部评论
(9) 回帖