腾讯笔试第四题——输出字符串

只过了60%,拜求大佬优化。
思路:
    如果字符串和输出字符串包含的元素种类不同,那么该字符串不可以;
    如果字符串长度大于输出字符串长度,那么该字符串不可以;
    其余情况,用暴力法判断:复制字符串 i 次,看是否可以满足题目条件。

n = int(input())   # 长度限制
T= input()   # 发送的字符串
m = int(input())   # 字符串个数
count = 0
for _ in range(m):
    strs = input()
    l = len(strs)
    if set(strs) != set(T):
        continue
    if l > len(T):
        continue
    else:
        i = 2
        strs0 = strs
        while len(strs) < n:
            strs = strs0 * i
            i += 1
        if strs[0:n] == T:
            count += 1
print(count)
#腾讯##笔试题目#
全部评论
我过了70%。。。 n = int(input()) T = input().strip() m = int(input()) choice = [] for _ in range(m):     choice.append(input().strip()) count = 0 for ele in choice:     time = n // len(ele) + 1     cur = ele * time     cur = cur[:n]     if cur == T:         count += 1 print(count)
点赞 回复 分享
发布于 2019-09-02 11:01
题目是啥啊?
点赞 回复 分享
发布于 2019-09-02 10:58

相关推荐

06-23 11:43
门头沟学院 Java
allin校招的烤冷...:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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