腾讯笔试第四题——输出字符串
只过了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)
#腾讯##笔试题目#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)