題目大意
給定乙個字串行和乙個字典,字典裡的每個單詞都在字串行中.
你可以在字串行中任意位置新增空格,使之構成句子.
輸出所有的句子.
比如:字串行 characters="catsanddog"
字典dictionary=["cats","and","sand","dog","cat"]
輸出:cats and dog
cat sand dog
思路回溯
**
#hadoop@dream:~/sohu$ cat last.py
#!/usr/bin/env python
class solution:
dictionary=list()
result=list()
def __int__(self):
pass
def __int__(self,dictionary):
self.dictionary=dictionary
def newdictionary(self,dictionary):
self.dictionary=dictionary
self.result=list()
def show(self):
if self.result:
print ' '.join(self.result)
else:
print "none"
def solve(self,characters):
characterslength=len(characters)
if characterslength==0:
self.show()
for word in self.dictionary:
if characters.startswith(word):
wordlength=len(word)
self.solve(characters[wordlength:characterslength])
self.result.pop()
if __name__=="__main__":
s=solution()
print "test case 0:"
characters=""
dictionary=list()
s.newdictionary(dictionary)
s.solve(dictionary)
print "test case 1:"
characters="catsanddog"
dictionary=["cats","and","sand","dog","cat"]
s.newdictionary(dictionary)
s.solve(characters)
print "test case 2:"
characters="aaa"
dictionary=["a","aa","aaa"]
s.newdictionary(dictionary)
s.solve(characters)
結果
hadoop@dream:~/sohu$./last.py
test case 0:
none
test case 1:
cats and dog
cat sand dog
test case 2:
a a a
a aa
aa a
aaa
搜狐筆試題 扎金花
遊戲規則 共52張普通牌,牌面為2,3,4,5,6,7,8,9,10,j,q,k,a之一,大小遞增,各四張 每人抓三張牌。兩人比較手中三張牌大小,大的人獲勝。對於牌型的規則如下 1.三張牌一樣即為豹子 2.三張牌相連為順子 a23不算順子 3.有且僅有兩張牌一樣為對子 豹子 順子 對子 普通牌型 在...
數字追趕 搜狐筆試題
題目如下 下面的for迴圈分別迴圈了幾次。1.unsigned short i,j for i 0,j 2 i j i 5,j 7 2.unsigned short i,j for i 3,j 7 i j i 3,j 7 分析 題目1 unsigned short 可以表示為2的16次方個數,其數的...
筆試題 馬戲團(搜狐)
輸入描述 首先乙個正整數n,表示人員個數。之後n行,每行三個數,分別對應馬戲團員編號,體重和身高。輸出描述 正整數m,表示羅漢塔的高度。輸入例子 6 1 65 100 2 75 80 3 80 100 4 60 95 5 82 101 6 81 70 輸出例子 4 用乙個vector 存放員工資料。...