-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrmm_word.py
More file actions
43 lines (41 loc) · 1.1 KB
/
rmm_word.py
File metadata and controls
43 lines (41 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
#coding=utf-8
import sys
import os
import string
reload(sys)
sys.setdefaultencoding('utf-8')
def create_table(inputStr):
wordsTable = set()
maxLength = 1
for word in inputStr.split():
word = unicode(word, 'utf-8')
wordsTable.add(word)
if maxLength < len(word):
maxLength = len(word)
return maxLength, wordsTable
def rmm_word_seg(sentence, maxLength, wordsTable):
end = len(sentence)
words = []
while end >= 0:
for begin in range(end - maxLength, end):
if sentence[begin:end] in wordsTable:
words.append(sentence[begin:end])
break
end = begin
return words
input1 = raw_input()
maxLength, wordsTable = create_table(input1)
input2 = raw_input()
input2 = unicode(input2)
while input2:
words = rmm_word_seg(input2, maxLength, wordsTable)
words.reverse()
for word in words:
word = word.encode('utf-8')
if words.index(word) < len(words) -1:
print word,
else:
print word
input2 = raw_input()
input2 = unicode(input2)