Problem:


MathJax.Hub.Config({
  tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});

B: ハミング距離 / Hamming Distance

問題文

Leading-zeros を含めた桁数が $N$ の，符号なし $2$ 進数表記された整数 $X$ がある．
$X$ とのハミング距離が $D$ となる $N$ 桁で $2$ 進数表記できる非負整数のうち，
値が最大のものを出力せよ．

$2$ 進数表記された整数間のハミング距離とは，
$2$ つの数において値が異なる桁の数である．例えば $000$ と $110$ のハミング距離は $2$ である．

入力

$N$

$X$

$D$

制約

$1 \leq N \leq 1000$

$0 \leq D \leq N$

入力は全て非負整数である

出力

答えの整数を符号なし $2$ 進数表記で $1$ 行に出力せよ．

サンプル

サンプル入力1

5
00001
3

サンプル出力1

11101

サンプル入力2

7
0110100
4

サンプル出力2

1111111

サンプル入力3

18
110001001110100100
6

サンプル出力3

111111111111100100

サンプル入力4

3
000
0

サンプル出力4

000



Sample input:
5
00001
3


Sample output:
11101

Submission status: Wrong Answer

Solution code:
n = int(input())
x = list(input())
d = int(input())
s = 0

while d > 0:
	if s == n:
		break
	if x[s] == "0":
		x[s] = "1"
		d = d - 1
	
	s = s + 1
	
if d == 0:
	print("".join(x))
else:
	while d > 0:
		x[n-1] = "0"
		n = n - 1
		d = d - 1
	print("".join(x))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array


def num_string_to_array(num_string):
    return array.array("B", map(int, num_string))


def num_array_to_string(num_array):
    return "".join(map(str, num_array))


def solve(n, x, d):
    rest_distance = d
    for i in range(n):
        if rest_distance <= 0:
            return x
        elif x[i] == 0:
            x[i] = 1
            rest_distance -= 1
    for j in range(n)[::-1]:
        if rest_distance <= 0:
            return x
        elif x[j] == 1:
            x[j] = 0
            rest_distance -= 1
    return x


def main():
    n = int(input())
    x = num_string_to_array(input())
    d = int(input())
    answer = num_array_to_string(solve(n, x[:], d))
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
_,x,d=[input() for _ in range(3)]
d=int(d);y=x=list(x)
for i in range(len(x)):
    if d and x[i]=='0':y[i]='1';d-=1
for i in range(len(x)-1,-1,-1):
    if d and x[i]=='1':y[i]='0';d-=1
print(*y,sep='')

Submission status: Wrong Answer

Solution code:
n,x,d=[input() for _ in range(3)]
d=int(d);y=x=list(x);n=int(n)
for i in range(n):
    if d and x[i]=='0':y[i]='1';d-=1
for i in range(n-1,-1,-1):
    if d and x[i]=='1':y[i]='0';d-=1
print(*y,sep='')

Submission status: Wrong Answer

Solution code:
n=int(input())
x=input()
d=int(input())
x=y=list(x)
for i in range(n):
    if d and x[i]=='0':y[i]='1';d-=1
while d:
    if x[i]=='1':y[i]='0';d-=1
print(*y, sep='')

Submission status: Wrong Answer

Solution code:
n = int(input())
bits_raw = input()
bits = [int(bits_raw[i]) for i in range(n)]
m = int(input())
j = 0
for i in range(m):
    while(True):
        if(j == n - 1):
            bits[j] = 1 - bits[j]
            break
        if(bits[j] == 0):
            bits[j] = 1
            break
        else:
            j += 1
print(''.join(map(str, bits)))

Submission status: Wrong Answer

Solution code:
n = int(input())
bits_raw = input()
bits = [int(bits_raw[i]) for i in range(n)]
m = int(input())
ones = []
zeros = []
for i in range(n):
    if(bits[i] == 1):
        ones.append(i)
    else:
        zeros.append(i)
if(len(zeros) < m):
    for i in range(m):
        bits[zeros[i]] = 1
    print(''.join(map(str, bits)))
else:
    allone = [1 for i in range(n)]
    l = len(ones)
    for i in range(m - len(zeros)):
        allone[ones[l - i]] = 0
    print(''.join(map(str, allone)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

N = int(input())
X = input()
D = int(input())

ans = list(X)
done = [False] * N
for i in range(N):
    if D == 0:
        break
    if ans[i] == "0":
        ans[i] = "1"
        done[i] = True
        D -= 1

for i in range(N)[::-1]:
    if D == 0:
        break
    if ans[i] == "1" and not done[i]:
        ans[i] = "0"
        D -= 1

print("".join(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array


def num_string_to_array(num_string):
    return array.array("B", map(int, num_string))


def num_array_to_string(num_array):
    return "".join(map(str, num_array))


def solve(n, x, d):
    answer = x[:]
    rest_distance = d
    for i in range(n):
        if rest_distance <= 0:
            return answer
        elif answer[i] == 0:
            answer[i] = 1
            rest_distance -= 1
    for j in range(n)[::-1]:
        if rest_distance <= 0:
            return answer
        elif answer[j] == 1 and answer[j] == x[j]:
            answer[j] = 0
            rest_distance -= 1
    return answer


def main():
    n = int(input())
    x = num_string_to_array(input())
    d = int(input())
    answer = num_array_to_string(solve(n, x[:], d))
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
x=input()
d=int(input())
x=list(x)
y=[1]*n
for i in range(n):
    if d and x[i]=='0':y[i]=0;x[i]='1';d-=1
i=n-1
while d:
    if y[i]:x[i]='0';d-=1
    i-=1
print(*x, sep='')

Submission status: Accepted

Solution code:
n = int(input())
bits_raw = input()
bits = [int(bits_raw[i]) for i in range(n)]
m = int(input())
ones = []
zeros = []
for i in range(n):
    if(bits[i] == 1):
        ones.append(i)
    else:
        zeros.append(i)
if(len(zeros) > m):
    for i in range(m):
        bits[zeros[i]] = 1
    print(''.join(map(str, bits)))
else:
    allone = [1 for i in range(n)]
    l = len(ones)
    for i in range(m - len(zeros)):
        allone[ones[l - i - 1]] = 0
    print(''.join(map(str, allone)))

Submission status: Accepted

Solution code:
n = int(input())
x = input()
d = int(input())
swap = {}
for i in range(n):
  if d == 0:break
  if x[i] == "0":
   swap[i] = True
   d -= 1

for i in range(n - 1, -1, -1):
  if d == 0:break
  if x[i] == "1":
    swap[i] = True
    d -= 1
print("".join([x[i] if i not in swap else ("0" if x[i] == "1" else "1") for i in range(n)]))