米哈游3.19笔试
第一题色盲问题,用了好几个dfs,过40%,提示超时
line1 = input().strip().split(' ')
m = int(line1[0])
n = int(line1[1])
graph = [['' for _ in range(n)] for _ in range(m)]
i = 0
while i < m:
line = input()
graph[i] = [m for m in line]
i += 1
R = 0
B = 0
G = 0
num1 = 0
num2 = 0
def dfs1(i, j):
if i < 0 or i >= m or j < 0 or j >= n:
return
if graph[i][j] != 'R':
return
graph[i][j] = 'D'
dfs1(i-1, j)
dfs1(i+1, j)
dfs1(i, j-1)
dfs1(i, j+1)
def dfs2(i, j):
if i < 0 or i >= m or j < 0 or j >= n:
return
if graph[i][j] != 'B':
return
graph[i][j] = 'A'
dfs2(i-1, j)
dfs2(i+1, j)
dfs2(i, j-1)
dfs2(i, j+1)
def dfs3(i, j):
if i < 0 or i >= m or j < 0 or j >= n:
return
if graph[i][j] != 'G':
return
graph[i][j] = 'A'
dfs3(i-1, j)
dfs3(i+1, j)
dfs3(i, j-1)
dfs3(i, j+1)
def dfs4(i, j):
if i < 0 or i >= m or j < 0 or j >= n:
return
if graph[i][j] != 'A':
return
graph[i][j] = 'Z'
dfs4(i-1, j)
dfs4(i+1, j)
dfs4(i, j-1)
dfs4(i, j+1)
for i in range(m):
for j in range(n):
if graph[i][j] == 'R':
dfs1(i, j)
R += 1
num1 += 1
elif graph[i][j] == 'B':
dfs2(i, j)
num1 += 1
elif graph[i][j] == 'G':
dfs3(i, j)
num1 += 1
for i in range(m):
for j in range(n):
if graph[i][j] == 'A':
dfs4(i, j)
num2 += 1
print( num1 - (num2+R))
第二题删除或增加s中的mhy,得到目标子串t,感觉思路和别人po出来的一样啊,只过了40%,有大佬能帮我看看问题吗
import collections
n = int(input())
i = 0
hash_table1 = collections.defaultdict(int)
hash_table2 = collections.defaultdict(int)
while True:
try:
s1 = input()
s2 = input()
t1 = ""
t2 = ""
m = len(s1) + 1
n = len(s2) + 1
for ch in s1:
if ch == 'm' or ch == 'h' or ch == 'y':
hash_table1[ch] += 1
else:
t1 += ch
for ch in s2:
if ch == 'm' or ch == 'h' or ch == 'y':
hash_table2[ch] += 1
else:
t2 += ch
if t1 == t2 and (hash_table1['m'] - hash_table2['m']) == (hash_table1['h'] - hash_table2['h']) and (hash_table1['y'] - hash_table2['y']) == (hash_table1['m'] - hash_table2['m']) and (hash_table1['y'] - hash_table2['y']) == (hash_table1['h'] - hash_table2['h']):
print("Yes")
else:
print("No")
except:
break
#24届实习生##我的实习求职记录#
查看20道真题和解析