codeforces167
codeforces167B
这道题的答案可以变成x + a + y,那么我们就通过遍历寻找最小长度的out就可以了
t = int(input())
for _ in range(t):
a = input()
b = input()
out = a + b
aa = len(a) + len(b)
for i in range(len(b) + 1):
j = i
for c in a:
if j < len(b) and c == b[j]:
j += 1
if aa + i - j < len(out):
out = b[:i] + a + b[j:]
print(len(out))
codeforces167C
这道题我们需要找到有几个是相同的1有几个是相同的-1,如果不相同或者相同的0,我们都要取最大的,最后一次遍历寻找两部电影中评分最小的与上一次的ans对比谁更大就取谁
from math import inf
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int, input().split()))
a1,b1 = 0,0
sua,sub = 0,0
for i in range(n):
if a[i] == b[i] == 0 or a[i] != b[i]:
if a[i] > b[i]:
sua += a[i]
else:
sub += b[i]
else:
if a[i] == b[i] == -1:
b1 += 1
else:
a1 += 1
ans = -inf
for i in range(-b1,a1 + 1):
ans = max(ans,min(sua + i,sub + (a1 - b1 - i)))
print(ans)
codeforces954
codeforces954C
- 对于每个位置 x 在列表 a 中:
- 将 s 中第 x-1 个位置(由于列表索引从0开始,而题目中位置从1开始,所以需要减1)的字符替换为 b 中对应位置的字符。
- 最后,将处理后的 s 打印出来,其中的字符直接连接在一起输出。
for _ in range(int(input())):
n,m = map(int, input().split())
s = list(input())
a = sorted(set(map(int, input().split())))
b = sorted(input())
i = 0
for x in a:
s[x-1] = b[i]
i+=1
s = ['a','b','b','v']
result = ''.join(s)
print(result)
codeforces954D
这道题我分了很多情况,首先是把所有最终结果是0的先进行输出然后进入下一个案例,剩下的最小只能是1或者是其他,然后再来一个循环来寻找这些数中的两位数,然后再进行遍历,如果这个数是1,那就加0,如果最终结果res是0,那就要把0变成1。
from math import inf
for _ in range(int(input())):
n = int(input())
a = input()
s = list(map(int,a.strip()))
ans = inf
if n == 2:
print(int(a))
continue
if (a[0] == '0' or a[2] == '0') and n == 3:
print(0)
continue
if '0' in a and n > 3:
print(0)
continue
for i in range(n - 1):
res = int(str(s[i]) + str(s[i + 1]))
if res == 1:
res = 0
j = 0
while j < n:
if j == i:
j += 2
continue
if s[j] != 1:
res += s[j]
j += 1
if res == 0:
res = 1
ans = min(ans,res)
print(ans)