鱼C论坛

 找回密码
 立即注册
查看: 2967|回复: 56

[已解决]Python:每日一题 368

[复制链接]
发表于 2020-4-4 20:08:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
今天的题目:


给定字符串 s 和字符串数组 words,求 words 中是 s 的子串的字符串个数。

示例:

输入:s = "abcde", words = ["a", "bb", "acd", "ace"]
输出:3
解释:有三个是 s 的子序列的单词:"a","acd",和 "ace" 。


欢迎大家一起答题!
最佳答案
2020-4-5 07:11:30
难度评级:简单
要素分析:字符串 查找
代码:
  1. def solve(s:str,words:list)->int:
  2.     res = 0
  3.     for word in words:
  4.         i = 0
  5.         for each in word:
  6.             i = s.find(each,i)+1
  7.             if not i:
  8.                 break
  9.         else:
  10.             res += 1
  11.     return res

  12. if __name__ == '__main__':
  13.     print('示例 输出:',solve('abcde',['a','bb','acd','ace']))
复制代码

忐忑.png
不知道超时否。

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-4 20:38:36 | 显示全部楼层
本帖最后由 March2615 于 2020-4-4 21:18 编辑

s里面是可以重复的?
比如输入:s=“abcdea”, words = ["cda"]
这时输出是1?

  1. def daily368(s: str, words: list) -> int:
  2.     ans = 0
  3.     word_list = {}
  4.     for word in words:
  5.         if word[0] not in word_list.keys():  # 需要一个key为26个字母、value为列表储存后续单词的字典
  6.             word_list[word[0]] = []
  7.         word_list[word[0]].append(word[1:])
  8.     for ch in s:
  9.         if ch in word_list:
  10.             ch_word = word_list[ch]  # ch_word是以ch为开头的单词
  11.             del word_list[ch]
  12.             for word in ch_word:
  13.                 if word == '':
  14.                     ans += 1
  15.                 else:
  16.                     if word[0] not in word_list.keys():
  17.                         word_list[word[0]] = []
  18.                     word_list[word[0]].append(word[1:])
  19.     return ans
复制代码


就按照我的想法来了
思路是把words按照首字母分类,首字母在s里面出现后,删除首字母,将剩余的单词重新分类,遍历一次s就可以得到答案

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 20:40:51 | 显示全部楼层
  1. def fun368(s,words):
  2.     cnt = 0
  3.     for each in words:
  4.         t = s[:]
  5.         for letter in each:            
  6.             if letter not in t:
  7.                 break
  8.             else:
  9.                 t = t.replace(letter,'')
  10.         else:            
  11.             cnt += 1
  12.     return cnt
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 20:41:48 | 显示全部楼层
本帖最后由 TJBEST 于 2020-4-4 21:08 编辑

先来一个效率低的
  1. def fun368(s,words):
  2.     def isSub(sub):
  3.         temp = s
  4.         for each in sub:
  5.             try:
  6.                 position = temp.index(each)
  7.                 temp = temp[(position+1):]
  8.             except Exception:
  9.                 return False
  10.         return True
  11.     result = 0
  12.     for eachword in words:
  13.         if isSub(eachword):
  14.             result += 1
  15.     return result
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 20:47:19 | 显示全部楼层
  1. def f368(s,words):
  2.     def find_str(s,str1):
  3.         index=-1
  4.         for i in str1:
  5.             index = s.find(i,index+1,len(s))
  6.             if index == -1:
  7.                 return False
  8.         else:
  9.             return True
  10.     count = 0
  11.     for j in words:
  12.         if find_str(s,j):
  13.             count += 1
  14.     return count
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 20:54:17 | 显示全部楼层
本帖最后由 ouyunfu 于 2020-4-4 21:47 编辑
  1. import re
  2. def f368(s,words):
  3.     count=0
  4.     for i in words:
  5.         s1 = s[:]
  6.         for m in i:
  7.             if m not in s1:
  8.                 break
  9.             else: s1=s1.replace(m,'')
  10.         else:
  11.             a=[max([i.start() for i in re.finditer(j,s)]) for j in i]
  12.             if sorted(a)==a:
  13.                 count+=1
  14.     return count
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 21:12:12 | 显示全部楼层
@zltzlt 我个人理解 子串的元素顺序得和s的一致吧 比如'asd'的子串不包括'sa'但是包含'ad'?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 21:20:57 From FishC Mobile | 显示全部楼层
本帖最后由 kinkon 于 2020-4-5 00:10 编辑

看例子感觉有顺序要求啊
  1. def f368(s, words):
  2.     out = 0
  3.     for w in words:
  4.         left = 0
  5.         bg = True
  6.         for x in w:               
  7.             tmp = s[left:].find(x)
  8.             if tmp == -1:
  9.                 bg = False
  10.                 break
  11.             else:
  12.                 left = tmp + 1
  13.         if bg:
  14.             out += 1
  15.     return out
复制代码

再来一个,试试哪个快
  1. from collections import Counter
  2. def f368(s, words):
  3.     s_cout = Counter(s)
  4.     out = 0
  5.     for w in words:
  6.         w_cout = Counter(w)
  7.         if all(s_cout[x] >= w_cout[x] for x in w_cout):
  8.             if list((s_cout + w_cout - s_cout).elements()) == list(w):              
  9.                 out += 1
  10.     return out
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 21:43:58 | 显示全部楼层
  1. def a368(s,words):
  2.     s,count = list(s),0
  3.     for i in words:
  4.         k = 0
  5.         for j in s:
  6.             if j == list(i)[k]:
  7.                 k += 1
  8.             if k == len(list(i)):
  9.                 count += 1
  10.                 break
  11.     print(count)
  12. a368('abcde',['a','bb','acd','ace'])
复制代码

评分

参与人数 1荣誉 +4 鱼币 +4 收起 理由
zltzlt + 4 + 4

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 22:44:38 | 显示全部楼层
  1. def fun368(s,words):

  2.     count = 0

  3.     def issubseq(s, t):

  4.         index = 0
  5.         for i in s:
  6.             index = t.find(i, index)
  7.             if index < 0:
  8.                 return False
  9.             index += 1

  10.         return True

  11.     for j in words:

  12.         if issubseq(j,s):
  13.             count += 1
  14.         else:
  15.             continue

  16.     return count
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 22:50:06 | 显示全部楼层
  1. def f368(s:str,words:list)->int:
  2.     count=0
  3.     for i in words:
  4.         m = 0
  5.         for j in s:
  6.             if j == i[m]:
  7.                 m += 1
  8.             if m == len(i):
  9.                 count += 1
  10.                 break
  11.     return count
复制代码

评分

参与人数 1荣誉 +4 鱼币 +4 收起 理由
zltzlt + 4 + 4

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 23:41:44 | 显示全部楼层
adc是不是acde的子序列
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-5 00:06:25 | 显示全部楼层
def func368(s,words):
    temp = set(s)
    dicts = {}
    count = 0
    for each in temp:
        dicts[each] = s.count(each)
    for i in range(len(words)):
        temp = set(words[i])
        for j in temp:
            if words[i].count(j) > dicts[j]:
                count -= 1
                break
        count += 1
    return count

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-5 00:11:49 | 显示全部楼层
fan1993423 发表于 2020-4-4 23:41
adc是不是acde的子序列

应该不是
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-5 07:11:30 | 显示全部楼层    本楼为最佳答案   
难度评级:简单
要素分析:字符串 查找
代码:
  1. def solve(s:str,words:list)->int:
  2.     res = 0
  3.     for word in words:
  4.         i = 0
  5.         for each in word:
  6.             i = s.find(each,i)+1
  7.             if not i:
  8.                 break
  9.         else:
  10.             res += 1
  11.     return res

  12. if __name__ == '__main__':
  13.     print('示例 输出:',solve('abcde',['a','bb','acd','ace']))
复制代码

忐忑.png
不知道超时否。

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-5 08:07:48 | 显示全部楼层
占楼
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-5 10:52:47 | 显示全部楼层
  1. def find_sub_str(string, str_list):
  2.     str_num = 0

  3.     def is_substring(str1, str2):
  4.         j = 0
  5.         for i in range(len(str2)):
  6.             find_position = str1.find(str2[i], j)
  7.             if find_position != -1:
  8.                 j = find_position + 1
  9.             else:
  10.                 return False
  11.         else:
  12.             return True
  13.     for i in range(len(str_list)):
  14.         if is_substring(string, str_list[i]):
  15.             str_num += 1
  16.     return str_num
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-5 11:19:13 | 显示全部楼层
  1. def f(s,words):
  2.     dict1 = {}
  3.     for each in s:
  4.         if each not in dict1.keys():
  5.             dict1[each] = 0
  6.         dict1[each] += 1
  7.     def f1(dict1,s):
  8.         for each in s:
  9.             if s.count(each) <= dict1[each]:
  10.                 s.replace('each','')
  11.             else:
  12.                 return False
  13.         return True
  14.     n = 0
  15.     for each in words:
  16.         if f1(dict1,each):
  17.             n += 1
  18.     return n
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-5 12:26:11 From FishC Mobile | 显示全部楼层
def f(s,w):
    for i in w:
        a=s.find(i)
        if a==-1:
            return 0
        s=s[a+1:]
    return 1

s = "abcde"
words = ["a", "bb", "acd", "ace"]
  
print(sum(f(s,w) for w in words))

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-5 14:26:37 | 显示全部楼层
  1. import re

  2. resultnum=0


  3. def just(s,word): #判断word是否是s的子字符串,是返回True,否返回False
  4.     length=len(word)
  5.     for i in range(length):
  6.        rflag=re.search(word[i],s)
  7.        if rflag==None:
  8.            return False
  9.            
  10.        t=rflag.span()
  11.        index=t[1]
  12.        s=s[index:]
  13.       
  14.     return True

  15. if __name__=='__main__':
  16.     enter=input("请输入检测的字符串,以‘,’隔开:")
  17.     s=input("请输入给定的字符串")
  18.     words=[word for word in enter.split(',')]
  19.     for each in words:
  20.         if just(s,each)==True:
  21.             resultnum+=1
  22.             print(each)

  23.     print (resultnum)
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
zltzlt + 5 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 18:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表