顺丰笔试
第一题,原始字符无顺序查找顺序字符'abcdefgh'的方案数,考完才看清楚题目,要去重。。。gg了
s = "aabhgfdec"
target = 'abcdefgh'
words = []
# 处理字符串
for j in range(len(s)):
if s[j] in target:
words.append(s[j])
if j!=0:
# 找到前缀,比如aabhgfdec中,遍历到b时,其前缀为a,那么就可以合并出新的字串ab
prex = words[-2]
if ord(prex[-1])+1 ==ord(s[j]):
for i in range(len(prex)):
words.append(prex[i:]+s[j])
# 去重
words = list(set(words))
# 排序
words.sort()
# 采用深度优先搜索
def count_ways_to_spelling(target, words):
def recursive_count(target, index):
if index == len(target):
return 1
ways = 0
for word in words:
if target[index:index + len(word)] == word:
ways += recursive_count(target, index + len(word))
return ways
return recursive_count(target, 0)
result = count_ways_to_spelling(target, words)
print("拼成 'abcdefgh' 的方案数:", result)
第二题,买糖果,有打折,没有思路。。。。有大佬会的指定下
阿里巴巴灵犀互娱公司福利 649人发布