第一题,原始字符无顺序查找顺序字符'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)第二题,买糖果,有打折,没有思路。。。。有大佬会的指定下
点赞 1
评论 2
全部评论

相关推荐

程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务