C. A-B Palindrome | Round #713 Div.3

LONGNEW·2021년 7월 26일
0

CP

목록 보기
64/155

https://codeforces.com/contest/1512/problem/A
시간 2초, 메모리 256MB

input :

  • t (1 ≤ t ≤ 104)
  • a b(0 ≤ a, b ≤ 2 * 105)
  • a + b 길이의 문자열

output :

  • "-1", if you can't replace all the characters '?' in the string s by '0' or '1' so that the string becomes a palindrome and that it contains exactly a characters '0' and exactly b characters '1';
    the string that is obtained as a result of the replacement, otherwise.
    '?'를 '0', '1'로 바꾸지 못하는 문자열의 경우, '0'이 a개 만큼 '1'이 b개 만큼 있는 문자열이 아닌 경우 "-1"을 출력하시오. 그렇지 않은 경우에는 새롭게 얻은 문자열을 출력하시오.

조건 :

  • You need to replace all the characters with '?' in the string s by '0' or '1' so that the string becomes a palindrome and has exactly a characters '0' and exactly b characters '1'. Note that each of the characters '?' is replaced independently from the others.
    '?'를 '0' 또는 '1'로 바꾸는데 이 때 palindrome을 유지해야 한다. 그리고 정확하게 '1'이 b개, '0'이 a개 만큼 존재해야 한다.

palindrome

팰린드롬인지 부터 확인해야 한다. ?가 있는 부분을 제외하고 우선적으로 팰린드롬이 아니라면 본인이 할 수 있는 것이 없다.

그것도 그런데 조건이 매우 많이 필요하다.

짝수, 홀수

팰린드롬 문자열의 경우 길이가 홀수, 짝수인 경우로 나눌 수 있다. 그리고
두 문자 중 하나만 ?인 경우에는 이미 정해졌다고 생각해야 한다. ?가 아닌 다른 문자를 집어 넣어주는 방식을 사용해야 한다.

둘 다 ?가 아닌 경우를 제외하고선 계속 해서 a, b의 개수를 줄이는 방식으로 카운트 하도록 하자.
그러니까 우선적으로 이미 고정되어 있는 문자들에 대해서 카운팅을 다 한후에 둘 다 ?인 것들을 채워가는 것이다.

홀수

?를 채워갈 때 남은 숫자가 홀수인 경우를 조심하자.
언제나 2개씩 사라지게 한다면 이게 발목을 잡을 수 있다.

import sys
import math

t = int(sys.stdin.readline())
for _ in range(t):
    temp_a, temp_b = map(int, sys.stdin.readline().split())
    a, b = temp_a, temp_b
    data = list(sys.stdin.readline().rstrip())
    right_idx, flag = len(data) - 1, 0

    for i in range(math.ceil(len(data) / 2)):
        if data[i] != '?' and data[right_idx] != '?':
            if data[i] != data[right_idx]:
                flag = 1
                break
        right_idx -= 1

    if (a % 2 == 1 and b % 2 == 1) or flag == 1:
        print(-1)
        continue

    right_idx = len(data) - 1
    for i in range(math.ceil(len(data) / 2)):
        if i == right_idx:
            if data[i] == '0':
                a -= 1
            elif data[i] == '1':
                b -= 1
            continue

        if data[i] != '?' and data[right_idx] == '?':
            if data[i] == '0':
                a -= 2
                data[right_idx] = '0'
            else:
                b -= 2
                data[right_idx] = '1'
        elif data[i] == '?' and data[right_idx] != '?':
            if data[right_idx] == '0':
                a -= 2
                data[i] = '0'
            else:
                b -= 2
                data[i] = '1'
        elif data[i] != '?' and data[right_idx] != '?':
            if data[i] == '0':
                a -= 2
            else:
                b -= 2

        right_idx -= 1
        
    right_idx = len(data) - 1
    for i in range(math.ceil(len(data) / 2)):
        if i == right_idx:
            if a > 0:
                data[i] = '0'
                a -= 1
            elif b > 0:
                data[i] = '1'
                b -= 1
            continue

        if data[i] == '?':
            if a > 1:
                data[i], data[right_idx] = '0', '0'
                a -= 2
            else:
                data[i], data[right_idx] = '1', '1'
                b -= 2

        right_idx -= 1

    if a == 0 and b == 0:
        for i in data:
            print(i, end="")
        print()
    else:
        print(-1)

깔끔하게 작성한 사람들도 있는 걸 보아하니 괜찮을 거 같긴 한데 지금은 말고 나중에... 한 번 해보도록 하겠다.

0개의 댓글