第一行两个整数
代表字符串长度和操作次数。
第二行一个长度为
的字符串
。
第三行一个长度为
的字符串
。
此后
行,每行一个整数
,表示每次交换的位置。
对于每一次询问,在一行上输出一个整数,表示交换后字符串
中的
子序列和
中
的子序列之差。
5 2 redar adade 5 4
1 2
n,q=map(int, input().split())
s=list(input())
t=list(input())
def findred(listf):
res=0
for i in range(1,len(listf)-1):
if listf[i]=='e':
res+=listf[:i].count('r')*listf[i:].count('d')
return res
for i in range(q):
c=int(input())-1
s[c],t[c]=t[c],s[c]
print(findred(s)-findred(t)) 9敏,超时了,家人们谁懂啊,一整个无语住了,我真的会谢(
line = input().split(" ")
length = int(line[0])
num = int(line[1])
line1 = input()
line2 = input()
def get_dp(line):
length = len(line)
dp_r = [0] * length # 当前下标前方有多少个r
dp_d = [0] * length # 当前下标后方有多少个d
dp_e = [] # 所有 e 的下标
for i in range(length - 1):
if line[i] == "r":
dp_r[i + 1] = dp_r[i] + 1
else:
dp_r[i + 1] = dp_r[i]
for i in range(length):
if line[i] == "e":
dp_e.append(i)
for i in range(length - 1, 0, -1):
if line[i] == "d":
dp_d[i - 1] = dp_d[i] + 1
else:
dp_d[i - 1] = dp_d[i]
return dp_r, dp_e, dp_d
def get_red_num(dp_r, dp_e, dp_d):
# print(dp_r, dp_e, dp_d)
res = 0
for index in dp_e:
res += dp_r[index] * dp_d[index]
return res
dp1_r, dp1_e, dp1_d = get_dp(line1)
dp2_r, dp2_e, dp2_d = get_dp(line2)
while 1:
# dp1_r_temp = list(dp1_r)
# dp1_e_temp = list(dp1_e)
# dp1_d_temp = list(dp1_d)
# dp2_r_temp = list(dp2_r)
# dp2_e_temp = list(dp2_e)
# dp2_d_temp = list(dp2_d)
dp1_r_temp = dp1_r
dp1_e_temp = dp1_e
dp1_d_temp = dp1_d
dp2_r_temp = dp2_r
dp2_e_temp = dp2_e
dp2_d_temp = dp2_d
try:
chage_index = int(input()) - 1
cha1 = line1[chage_index]
cha2 = line2[chage_index]
if cha1 == "r" and cha2 != "r":
for i in range(chage_index + 1, length):
dp1_r_temp[i] -= 1
dp2_r_temp[i] += 1
if cha1 == "e" and cha2 != "e":
dp1_e_temp.remove(chage_index)
dp2_e_temp.append(chage_index)
if cha1 == "d" and cha2 != "d":
for i in range(0, chage_index):
dp1_d_temp[i] -= 1
dp2_d_temp[i] += 1
if cha2 == "r" and cha1 != "r":
for i in range(chage_index + 1, length):
dp2_r_temp[i] -= 1
dp1_r_temp[i] += 1
if cha2 == "e" and cha1 != "e":
dp2_e_temp.remove(chage_index)
dp1_e_temp.append(chage_index)
if cha2 == "d" and cha1 != "d":
for i in range(0, chage_index):
dp2_d_temp[i] -= 1
dp1_d_temp[i] += 1
print(get_red_num(dp1_r_temp, dp1_e_temp, dp1_d_temp) - get_red_num(dp2_r_temp, dp2_e_temp, dp2_d_temp))
except Exception as e:
# print(e)
break